Skip to content

Commit 83bd6cb

Browse files
committed
Added module
1 parent 23f8abb commit 83bd6cb

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

docs/basic.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ Basic
44
Snippets on basic things.
55

66

7+
not None test
8+
-------------
9+
10+
::
11+
12+
if value is not None:
13+
pass
14+
15+
716
`ternary conditional operator`_
817
-------------------------------
918

@@ -24,5 +33,25 @@ Snippets on basic things.
2433
print idx, val
2534

2635

36+
`how do I check if a variable exists`_
37+
--------------------------------------
38+
39+
To check the existence of a local variable::
40+
41+
if 'myVar' in locals():
42+
# myVar exists.
43+
44+
To check the existence of a global variable::
45+
46+
if 'myVar' in globals():
47+
# myVar exists.
48+
49+
To check if an object has an attribute::
50+
51+
if hasattr(obj, 'attr_name'):
52+
# obj.attr_name exists.
53+
54+
2755
.. _ternary conditional operator: http://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator
2856
.. _accessing the index in for loops: http://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops
57+
.. _how do I check if a variable exists: http://stackoverflow.com/questions/843277/how-do-i-check-if-a-variable-exists-in-python

docs/file.rst

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
File
22
====
33

4-
Snippets about files manipulation.
4+
Snippets about files and folders manipulation.
55

66

77
`check if file exists`_
@@ -32,6 +32,38 @@ It is easier to use the open method from the codecs module::
3232
filename, ext = os.path.splitext('/path/to/somefile.ext')
3333

3434

35+
`get file creation & modification date/times`_
36+
----------------------------------------------
37+
38+
ctime() does not refer to creation time on `*nix` systems, but rather the last
39+
time the inode data changed::
40+
41+
import os.path, time
42+
print "last modified: %s" % time.ctime(os.path.getmtime(file))
43+
print "created: %s" % time.ctime(os.path.getctime(file))
44+
45+
46+
`find current directory and file's directory`_
47+
----------------------------------------------
48+
49+
::
50+
os.getcwd()
51+
os.path.dirname(os.path.realpath(__file__))
52+
53+
54+
`remove/delete a folder that is not empty`_
55+
-------------------------------------------
56+
57+
::
58+
59+
import shutil
60+
61+
shutil.rmtree('/folder_name')
62+
63+
3564
.. _check if file exists: http://stackoverflow.com/questions/82831/how-do-i-check-if-a-file-exists-using-python
3665
.. _unicode (utf8) reading and writing to files: http://stackoverflow.com/questions/491921/unicode-utf8-reading-and-writing-to-files-in-python
3766
.. _extracting extension from filename: http://stackoverflow.com/questions/541390/extracting-extension-from-filename-in-python
67+
.. _remove/delete a folder that is not empty: http://stackoverflow.com/questions/303200/how-do-i-remove-delete-a-folder-that-is-not-empty-with-python
68+
.. _get file creation & modification date/times: http://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python
69+
.. _find current directory and file's directory: http://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Overflow, others are written by me or taken from various web resources.
2020
method
2121
metaclass
2222
performance
23+
module
2324
io
2425
file
2526
email

docs/module.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Module
2+
======
3+
4+
Snippets on packages and modules.
5+
6+
7+
get all module files
8+
--------------------
9+
10+
::
11+
12+
def _iter_module_files():
13+
for module in sys.modules.values():
14+
filename = getattr(module, '__file__', None)
15+
if filename:
16+
if filename[-4:] in ('.pyc', '.pyo'):
17+
filename = filename[:-1]
18+
yield filename

0 commit comments

Comments
 (0)