LoginSignup
2
2

More than 5 years have passed since last update.

PythonとClojureのよく使う使う処理の対比表

Last updated at Posted at 2015-11-20

個人的なメモです。

文字列処理

文字列をスペースで分割する

(require '[clojure.string :as str])
(str/split "spam egg ham" #" ")
; ["spam" "egg" "ham"]
>>> "spam egg ham".split()
['spam', 'egg', 'ham']

Unicodeコードポイントを取得

(int (.charAt "酒" 0)
; 37202
(format "%x" (int (.charAt "酒" 0)))
; "9152"
>>> ord(u"酒")
37202
>>> hex(ord(u"酒"))
'0x9152'

ファイル処理

テンポラリディレクトリを取得

Clojure

user=> (System/getProperty "java.io.tmpdir")
"/tmp"

Clojureならばfsを使うのがよりClojureらしいと思われる。

user=> (require '[me.raynes.fs :as fs])
nil
user=> (fs/tmpdir)
"/tmp"

Python

>>> import tempfile
>>> tempfile.gettempdir()
'/tmp'

時刻処理

標準ライブラリだけで頑張るのではなく、Pythonだとdateutil, Clojureだとclj-timeを使うのが良いのではないだろうか。

ISO形式の文字列からUnixタイムスタンプを取得する

(require '[clj-time.coerce :as c])
(require '[clj-time.local :as l])
(/ (c/to-long (l/to-local-date-time "2015-11-24T22:00:00+09:00")) 1000)
; 1448370000
>>> import dateutil.parser
>>> import time
>>> time.mktime(dateutil.parser.parse("2015-11-24T22:00:00+09:00").utctimetuple())
1448370000.0

Unixタイムスタンプから時刻オブジェクトを生成

(require '[clj-time.coerce :as c])
(c/from-long (* 1448370000 1000))
; #object[org.joda.time.DateTime 0x6f5185fe "2015-11-24T13:00:00.000Z"]
>>> import datetime
>>> datetime.datetime.utcfromtimestamp(1448370000.0)
datetime.datetime(2015, 11, 24, 13, 0)
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2