Skip to content

Commit d55d388

Browse files
author
Shakir
committed
str
1 parent 9e3eb1a commit d55d388

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

_posts/python/2020-06-14-python-string-methods.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,25 @@ with '$' in the string and check the memory location again
160160
Thi$ Earth i$ $o Huge !!!
161161
```
162162

163+
### Partition
164+
165+
We can parition a string in to a three element tuple, based on a pattern we search.
166+
```
167+
>>> p = 'Micky and Mouse'.partition('and')
168+
>>> print(type(p))
169+
<class 'tuple'>
170+
>>> print(len(p))
171+
3
172+
>>> print(p[0], p[1], p[2])
173+
Micky and Mouse
174+
>>> print(p[0], p[1], p[-1])
175+
Micky and Mouse
176+
>>> print(p)
177+
('Micky ', 'and', ' Mouse')
178+
```
179+
The string above was partioned in to a tuple containing three string, the middle string is however the string we are searching, in this case
180+
'and'.
181+
163182
### Join
164183
This is used to combine multiple strings into one string.
165184
```

_posts/python/2020-10-20-python-import-modules.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ We can append any extra paths using the append method.
3131
The last entry in the path would be /tmp/ as we just appended it.
3232

3333
We may also print the list of all modules including 'sys' using the 'modules' attribute.
34+
```
3435
>>> print(sys.modules)
3536
{'sys': <module 'sys' (built-in)>, 'builtins': <module 'builtins' (built-in)>, '_frozen_importlib': <module 'importlib._bootstrap' (frozen)>, '_imp': <module '_imp' (built-in)>, '_warnings': <module '_warnings' (built-in)>, '_frozen_importlib_external': <module 'importlib._bootstrap_external' (frozen)>, '_io': <module 'io' (built-in)>, 'marshal': <module 'marshal' (built-in)>, 'posix': <module 'posix' (built-in)>, '_thread': <module '_thread' (built-in)>, '_weakref': <module '_weakref' (built-in)>, 'time': <module 'time' (built-in)>, 'zipimport': <module 'zipimport' (frozen)>, '_codecs': <module '_codecs' (built-in)>, 'codecs': <module 'codecs' from '/usr/lib/python3.8/codecs.py'>,
3637
--TRUNCATED--

_posts/python/2020-10-25-python-ways-to-run-code.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,52 @@ $ python3 __pycache__/script.cpython-38.pyc
9999
Hello World!
100100
```
101101

102+
Here is an alternative way of compiling using code using the compile method of py_compile, instead of a single command via cli.
103+
```
104+
$ python3
105+
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
106+
[GCC 9.3.0] on linux
107+
Type "help", "copyright", "credits" or "license" for more information.
108+
>>> import py_compile
109+
>>> py_compile.compile('script.py')
110+
'__pycache__/script.cpython-38.pyc'
111+
>>> exit()
112+
113+
$ ls __pycache__/
114+
script.cpython-38.pyc
115+
```
116+
117+
We can also compile all files in the directory at once.
118+
119+
Let's say we have two scripts as follows.
120+
```
121+
$ ls
122+
script1.py script2.py
123+
124+
$ cat script1.py
125+
print('Hello')
126+
127+
$ cat script2.py
128+
print('World!')
129+
```
130+
131+
We can compile both at the same time.
132+
```
133+
$ python3 -m compileall .
134+
Listing '.'...
135+
Compiling './script1.py'...
136+
Compiling './script2.py'...
137+
```
138+
139+
The \_\_pycache\_\_ directory should have pycs for both.
140+
```
141+
$ ls
142+
__pycache__ script1.py script2.py
143+
144+
$ ls __pycache__/
145+
script1.cpython-38.pyc script2.cpython-38.pyc
146+
```
147+
102148
## Virtual Environment
103149
We can create virtual environments, which creates a complete copy of the python interpreter, any modules or packages installed inside the virtual
104150
environment, stays with in it and doesn't affect rest of the system. This is very useful when you have multiple projects and you don't want

0 commit comments

Comments
 (0)