-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathatom.xml
629 lines (494 loc) · 41.8 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Nazarii Gudzovatyi]]></title>
<link href="http://Nazarii.github.io/atom.xml" rel="self"/>
<link href="http://Nazarii.github.io/"/>
<updated>2016-09-01T13:17:22+03:00</updated>
<id>http://Nazarii.github.io/</id>
<author>
<name><![CDATA[Nazarii]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[Interview questions]]></title>
<link href="http://Nazarii.github.io/blog/2016/08/15/interview-questions/"/>
<updated>2016-08-15T17:44:00+03:00</updated>
<id>http://Nazarii.github.io/blog/2016/08/15/interview-questions</id>
<content type="html"><![CDATA[<p>Going to the technical interview is always a big challenge and great experience for me (I should mention, I’ve been at < 5 interviews for all of my life). Here I’m posting Interview questions, I’ve been asked at these meetings. I’ve been noticing unique question’s after each interview and I wrote them down to my Evernote so I could methodize and teach them in the future. I will update this list, once I’ve been asking for new ones.<!-- more --></p>
<p><strong>Python</strong></p>
<ul>
<li>Abstract class</li>
<li>Metaclass</li>
<li>old-style classes vs new classes</li>
<li>Decorators, usage, @staticmethod, @classmethod</li>
<li>List comprehensions & generators, differences</li>
<li>Context managers</li>
<li>Multithreading (Mutex/Semaphore/Lock/RLock)</li>
<li>Sockets</li>
<li>Http methods</li>
<li>Setuptools</li>
<li>Magic methods: When is def <strong>call</strong>() used?</li>
<li><strong>new</strong>() method? Why is it for? When called? Difference to <strong>init</strong>().</li>
<li><strong>slots</strong>: why for and when used;</li>
<li>frozenset</li>
<li>global local</li>
<li>Http methods (When POST and GET are used?)</li>
<li>REST</li>
<li>JSON</li>
<li>Python – serialisation (pickle)</li>
<li>Methods overloading</li>
<li>How to see all inheritance order (<strong>mro</strong>)</li>
<li>x = A(); type(a); isinstance(x, A)</li>
<li>def test(self, l=[]): print l</li>
<li>OrderedDict</li>
<li>Introspection (inspect, dir())</li>
<li>Debugging/Profiling (pdb, cProfile)</li>
<li>Write regexp to find all floats in string</li>
</ul>
<p><strong>Unix & Networking</strong></p>
<ul>
<li>How to find the file?</li>
<li>How to search in the file?</li>
<li>Get dynamic output from the file.</li>
<li>Kill all python processes</li>
<li>Kill vs kill -9</li>
<li>Sigterm, signals</li>
<li>pop3 vs IMAP</li>
<li>What is IP?</li>
<li>Http protocol</li>
<li>What is HEAD http request for?</li>
<li>Client want’s to use only GET requests in his REST-API, will you agree? What will you advice?</li>
<li>TCP vs HTTPS</li>
<li>UDP vs TCP</li>
<li>How does SSL works</li>
<li>Apache vs nginx; configuration</li>
<li>REST</li>
<li>Bash scripting</li>
<li>Creating unix service/daemon</li>
<li>Process vs Thread: differences</li>
</ul>
<p><strong>DB</strong></p>
<ul>
<li>Normalisation</li>
<li>ACID</li>
<li>Primary key/Foreign key/ Indexes</li>
<li>Subquery vs Join speed</li>
<li>SQL Search algorithm: get 499 element from 1000</li>
<li>Inner, Outer, Left, Right Joins</li>
<li>SQL many2many relationship</li>
<li>SQL Group by vs HAVING</li>
<li>What does ‘No SQL’ mean? Why do is it different from relational DB?</li>
<li>NoSQL vs SQL: use cases, differences.</li>
</ul>
<p><strong>GENERAL</strong></p>
<ul>
<li>OOP principles, examples.</li>
<li>Design patterns</li>
<li>SOLID</li>
<li>What is ‘prototype’ in JS?</li>
<li>Hash table</li>
<li>Continuous integration</li>
<li>Functional & Non-functional requirements</li>
<li>Agile principles</li>
<li>Estimation process: How does it look?</li>
<li>What is MVC? Why do we need to separate View from business logic?</li>
<li>GIL – what is it? How doee it work’s?</li>
</ul>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Understanding Decorator's in Python]]></title>
<link href="http://Nazarii.github.io/blog/2015/01/26/decorator-basics-in-python/"/>
<updated>2015-01-26T18:00:00+02:00</updated>
<id>http://Nazarii.github.io/blog/2015/01/26/decorator-basics-in-python</id>
<content type="html"><![CDATA[<p>Bellow is the <a href="http://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python/1594484#1594484">answer</a> from the StackOverflow community wiki which explain’s python decorators in the clear manner. I’ve decided to have it in my bookmarks here as It is the best explanation that I’ve seen before. It is especially useful for the devs that haven’t used decorator’s before<!--more--></p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Proxylocal - share your local web server via Internet]]></title>
<link href="http://Nazarii.github.io/blog/2014/10/09/proxylocal-share-your-local-web-server-via-internet/"/>
<updated>2014-10-09T23:33:00+03:00</updated>
<id>http://Nazarii.github.io/blog/2014/10/09/proxylocal-share-your-local-web-server-via-internet</id>
<content type="html"><![CDATA[<p><a href="http://proxylocal.com/">ProxyLocal</a> is a tool that runs on the command line, It could proxy your local web-server and make it publicly available over the internet. Really usefull tool for showing demos with local access to the server.</p>
<p>Assume you are running your local web-server on port 8069. To make it publicly available run:</p>
<figure class='code'><figcaption><span>bash</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>proxylocal 8069 --host demo32
</span><span class='line'>Local server on port 8069 is now publicly available via:
</span><span class='line'>http://demo32.t.proxylocal.com/
</span></code></pre></td></tr></table></div></figure>
<!-- more -->
<h2>Installation</h2>
<p>On any system with <a href="http://www.ruby-lang.org/en/downloads/">ruby</a> and <a href="https://rubygems.org/pages/download">rubygems</a> installed, open your terminal and type:</p>
<figure class='code'><figcaption><span>bash</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>gem install proxylocal
</span></code></pre></td></tr></table></div></figure>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Postgres: find the biggest tables]]></title>
<link href="http://Nazarii.github.io/blog/2014/01/10/postgres-find-the-biggest-tables/"/>
<updated>2014-01-10T21:18:00+02:00</updated>
<id>http://Nazarii.github.io/blog/2014/01/10/postgres-find-the-biggest-tables</id>
<content type="html"><![CDATA[<p>Usefull query to find 20 biggest tables in your PostgreSQL database:<!--more--></p>
<figure class='code'><figcaption><span>SQL</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>SELECT nspname || '.' || relname AS "relation",
</span><span class='line'> pg_size_pretty(pg_relation_size(C.oid)) AS "size"
</span><span class='line'> FROM pg_class C
</span><span class='line'> LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
</span><span class='line'> WHERE nspname NOT IN ('pg_catalog', 'information_schema')
</span><span class='line'> ORDER BY pg_relation_size(C.oid) DESC
</span><span class='line'> LIMIT 20;</span></code></pre></td></tr></table></div></figure>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Make SQL query looks nice]]></title>
<link href="http://Nazarii.github.io/blog/2013/12/29/make-sql-query-looks-nice/"/>
<updated>2013-12-29T15:19:00+02:00</updated>
<id>http://Nazarii.github.io/blog/2013/12/29/make-sql-query-looks-nice</id>
<content type="html"><![CDATA[<p>Found nice resource for SQL formatting: <a href="http://sqlformat.appspot.com/">http://sqlformat.appspot.com/</a> . Service allows you to get nice SQL output from existing query quickly<!--more-->, so formatting:</p>
<figure class='code'><figcaption><span>SQL</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>select *
</span><span class='line'>from foo
</span><span class='line'>join bar on val1 = val2
</span><span class='line'>where id = 123;</span></code></pre></td></tr></table></div></figure>
<p>will result in</p>
<figure class='code'><figcaption><span>SQL</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>SELECT *
</span><span class='line'>FROM foo
</span><span class='line'>JOIN bar ON val1 = val2
</span><span class='line'>WHERE id = 123;</span></code></pre></td></tr></table></div></figure>
<p>which looks nice and more readable, especially when you have long queries.
Basicly service is demonstration of Python library python-sqlparse which is more pleasant. Enjoy it!</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Git: Find out who broke functionality]]></title>
<link href="http://Nazarii.github.io/blog/2013/10/18/git-find-out-who-broke-functionality/"/>
<updated>2013-10-18T16:15:00+03:00</updated>
<id>http://Nazarii.github.io/blog/2013/10/18/git-find-out-who-broke-functionality</id>
<content type="html"><![CDATA[<p>Not so far ago I came accross an issue when I noticed that functionality that was worked previously is broken at the moment and is not working (<em>Yes, I know we should use regression testing but at the moment we are implementing it</em>). —To be continued—</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Moving Virtual Box machine to another computer]]></title>
<link href="http://Nazarii.github.io/blog/2013/10/18/moving-virtual-box-machine-to-another-computer/"/>
<updated>2013-10-18T15:58:00+03:00</updated>
<id>http://Nazarii.github.io/blog/2013/10/18/moving-virtual-box-machine-to-another-computer</id>
<content type="html"><![CDATA[<p>From time to time I’m using <a href="https://www.virtualbox.org/">VirtualBox</a> to launch Windows machines and here is a simple way of taking existing VM copy and restore it on another instance from command line:<!-- more --></p>
<figure class='code'><figcaption><span>bash</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>VBoxManage <span class="nb">export</span> <VM name> -o ~/exp.ova
</span></code></pre></td></tr></table></div></figure>
<p>Copy newly created exp.ova file to another instance and to restore it run:</p>
<figure class='code'><figcaption><span>bash</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>VBoxManage import ~/exp.ova
</span></code></pre></td></tr></table></div></figure>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Python: starting http server in one line]]></title>
<link href="http://Nazarii.github.io/blog/2013/07/28/python-starting-http-server-in-one-command/"/>
<updated>2013-07-28T22:21:00+03:00</updated>
<id>http://Nazarii.github.io/blog/2013/07/28/python-starting-http-server-in-one-command</id>
<content type="html"><![CDATA[<p>The idea is taken from it’s own post on <a href="http://habrahabr.ru/post/118460/">habrahabr</a>.<!-- more -->
We need to share 2GB file throw our local network
and the quickest and simpliest way by advice of our <a href="http://www.linkedin.com/profile/view?id=98165358&authType=name&authToken=oGVS&goback=">Joda</a> was to start local http server with the help of Python. To do it simply type in console:</p>
<figure class='code'><figcaption><span>bash</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>python -m CGIHTTPServer
</span></code></pre></td></tr></table></div></figure>
<p>This will start http server on default 8000 port and after opening you browser on <strong><a href="http://localhost:8000">http://localhost:8000</a></strong> you should see a list of files in directory you prompted command from. Copy link address of file you need to and share with other members of you network. Enjoy fast download speed with python!</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Accessing ftp server with SSL/TLS enabled using python 2.6]]></title>
<link href="http://Nazarii.github.io/blog/2013/07/11/accessing-ftp-server-with-ssl-slash-tsl-enabled-with-python-2-dot-6/"/>
<updated>2013-07-11T22:06:00+03:00</updated>
<id>http://Nazarii.github.io/blog/2013/07/11/accessing-ftp-server-with-ssl-slash-tsl-enabled-with-python-2-dot-6</id>
<content type="html"><![CDATA[<p>Just want to provide a solution of problem I’ve faced, maybe it will be usefull for someone.
Issue is next:</p>
<blockquote><p>We need to connect to remote ftp server, download and delete existing files.</p></blockquote>
<!-- more -->
<p>Look’s easy, let’s use standard library <a href="http://docs.python.org/2/library/ftplib.html">ftplib</a>:</p>
<figure class='code'><figcaption><span>sublime-text</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">ftplib</span> <span class="kn">import</span> <span class="n">FTP</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">os</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">passwd</span><span class="p">,</span> <span class="n">directory_path</span><span class="o">=</span><span class="s">'/'</span><span class="p">):</span>
</span><span class='line'> <span class="n">connection</span> <span class="o">=</span> <span class="n">FTP</span><span class="p">(</span><span class="n">host</span><span class="o">=</span><span class="n">host</span><span class="p">,</span> <span class="n">user</span><span class="o">=</span><span class="n">username</span><span class="p">,</span> <span class="n">passwd</span><span class="o">=</span><span class="n">passwd</span><span class="p">)</span>
</span><span class='line'> <span class="n">filenames</span> <span class="o">=</span> <span class="n">connection</span><span class="o">.</span><span class="n">nlst</span><span class="p">()</span>
</span><span class='line'> <span class="n">file_counter</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class='line'> <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">filenames</span><span class="p">:</span>
</span><span class='line'> <span class="n">file_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">directory_path</span><span class="p">,</span> <span class="n">file_counter</span><span class="p">)</span> <span class="c"># Supposed you use Postfix os, on windows locations would be different</span>
</span><span class='line'> <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">file_path</span><span class="p">,</span> <span class="s">'wb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">output_file</span><span class="p">:</span>
</span><span class='line'> <span class="n">connection</span><span class="o">.</span><span class="n">retrbinary</span><span class="p">(</span><span class="s">'RETR </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="n">filename</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">data</span><span class="p">:</span> <span class="n">output_file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
</span><span class='line'> <span class="n">output_file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</span><span class='line'> <span class="n">connection</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
</span><span class='line'> <span class="n">counter</span> <span class="o">+=</span> <span class="mi">1</span>
</span><span class='line'> <span class="n">connection</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
</span><span class='line'> <span class="k">return</span> <span class="bp">True</span>
</span></code></pre></td></tr></table></div></figure>
<p>Code was working nicely untill one moment i get exception <i><font color="red">530 SSL required</font></i>.
The deal is because of user configuration on remote server, it is set to reguire <a href="http://en.wikipedia.org/wiki/Transport_Layer_Security">SSL/TLS</a> encryption. But standard ftplib in my version of python 2.6.5 doesn’t provide solution to connect with SSL/TLS, it was added in python 2.7. Version update looks bad, external libraries i also dont want to use. So i decided to get ftplib module from 2.7 version and use in my code.<strong>(Download <a href="http://www.python.org/download/releases/2.7/">2.7 version</a> and locate ftplib.py file)</strong> After replacing standard ftplib file in you pythonpath with one from 2.7 our code looks like:</p>
<figure class='code'><figcaption><span>sublime-text</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">ftplib</span> <span class="kn">import</span> <span class="n">FTP_TLS</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">os</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">passwd</span><span class="p">,</span> <span class="n">directory_path</span><span class="o">=</span><span class="s">'/'</span><span class="p">):</span>
</span><span class='line'> <span class="n">connection</span> <span class="o">=</span> <span class="n">FTP_TLS</span><span class="p">(</span><span class="n">host</span><span class="o">=</span><span class="n">config_obj</span><span class="o">.</span><span class="n">host</span><span class="p">,</span> <span class="n">user</span><span class="o">=</span><span class="n">config_obj</span><span class="o">.</span><span class="n">username</span><span class="p">,</span> <span class="n">passwd</span><span class="o">=</span><span class="n">config_obj</span><span class="o">.</span><span class="n">passwd</span><span class="p">)</span>
</span><span class='line'> <span class="n">connection</span><span class="o">.</span><span class="n">prot_p</span><span class="p">()</span>
</span><span class='line'> <span class="n">filenames</span> <span class="o">=</span> <span class="n">connection</span><span class="o">.</span><span class="n">nlst</span><span class="p">()</span>
</span><span class='line'> <span class="n">file_counter</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class='line'> <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">filenames</span><span class="p">:</span>
</span><span class='line'> <span class="n">file_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">directory_path</span><span class="p">,</span> <span class="n">file_counter</span><span class="p">)</span>
</span><span class='line'> <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">file_path</span><span class="p">,</span> <span class="s">'wb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">output_file</span><span class="p">:</span>
</span><span class='line'> <span class="n">connection</span><span class="o">.</span><span class="n">retrbinary</span><span class="p">(</span><span class="s">'RETR </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="n">filename</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">data</span><span class="p">:</span> <span class="n">output_file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
</span><span class='line'> <span class="n">output_file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</span><span class='line'> <span class="n">connection</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
</span><span class='line'> <span class="n">counter</span> <span class="o">+=</span> <span class="mi">1</span>
</span><span class='line'> <span class="n">connection</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
</span><span class='line'> <span class="k">return</span> <span class="bp">True</span>
</span></code></pre></td></tr></table></div></figure>
<p><em>Also I want to notice if you do not want to break anything with further using ftplib library after replacing, my suggestion is to not replace files but to use file as separate module(e.g. if you run code from your own package copy downloaded file in it and include in <strong><strong>init</strong>.py</strong>, the in code where you want to use it import like <strong>from <your_module_name> import ftplib as ftplib2.7</strong>).</em></p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Calculating working days with python.]]></title>
<link href="http://Nazarii.github.io/blog/2013/06/29/calculating-working-days-with-python/"/>
<updated>2013-06-29T21:15:00+03:00</updated>
<id>http://Nazarii.github.io/blog/2013/06/29/calculating-working-days-with-python</id>
<content type="html"><![CDATA[<p>So the specification is following:</p>
<blockquote><p>We need to calculate number of working days between two dates - it mean’s we need to calculate all days excluding Saturday and Sunday. If the function is running after 6:00 pm this working day need to be also excluded.</p></blockquote>
<!-- more -->
<p>After some googling I’ve found this task could be resolved using rrule module.
Be sure you have <a href="http://labix.org/python-dateutil">dateutil</a> library installed to use this example.</p>
<figure class='code'><figcaption><span>sublime-text</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">import</span> <span class="nn">time</span>
</span><span class='line'><span class="kn">from</span> <span class="nn">dateutil</span> <span class="kn">import</span> <span class="n">rrule</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">get_working_days</span><span class="p">(</span><span class="n">date_start_obj</span><span class="p">,</span> <span class="n">date_end_obj</span><span class="p">):</span>
</span><span class='line'> <span class="n">weekdays</span> <span class="o">=</span> <span class="n">rrule</span><span class="o">.</span><span class="n">rrule</span><span class="p">(</span><span class="n">rrule</span><span class="o">.</span><span class="n">DAILY</span><span class="p">,</span> <span class="n">byweekday</span><span class="o">=</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span> <span class="n">dtstart</span><span class="o">=</span><span class="n">date_start_obj</span><span class="p">,</span> <span class="n">until</span><span class="o">=</span><span class="n">date_end_obj</span><span class="p">)</span>
</span><span class='line'> <span class="n">weekdays</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">weekdays</span><span class="p">))</span>
</span><span class='line'> <span class="k">if</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s">'%H'</span><span class="p">))</span> <span class="o">>=</span> <span class="mi">18</span><span class="p">:</span>
</span><span class='line'> <span class="n">weekdays</span> <span class="o">-=</span> <span class="mi">1</span>
</span><span class='line'> <span class="k">return</span> <span class="n">weekdays</span>
</span></code></pre></td></tr></table></div></figure>
<p>Here byweekday parameter is a list of days of week we want to caculate, ti’s elements can be integers (Monday is 0, Tuesday is 1 etc) or rrule objects – rrule.MO, rrule.TU etc. Other usefull methods for date (e.g. full weeks) calculations could be found <a href="http://labix.org/python-dateutil">here</a>.</p>
<p>Suppose we need to calculate days from 1-01-2013 until 28-07-2013.
Our code will be:</p>
<figure class='code'><figcaption><span>sublime-text</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">date</span>
</span><span class='line'>
</span><span class='line'><span class="n">date_start_obj</span> <span class="o">=</span> <span class="n">date</span><span class="p">(</span><span class="mi">2013</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
</span><span class='line'><span class="n">date_end_obj</span> <span class="o">=</span> <span class="n">date</span><span class="p">(</span><span class="mi">2013</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">18</span><span class="p">)</span>
</span><span class='line'><span class="k">print</span> <span class="n">get_working_days</span><span class="p">(</span><span class="n">date_start_obj</span><span class="p">,</span> <span class="n">date_end_obj</span><span class="p">):</span>
</span></code></pre></td></tr></table></div></figure>
<p>The output will be: <strong>142</strong> (Calculate yourself if don’t trust me).</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Few SSH productivity tips for newbies.]]></title>
<link href="http://Nazarii.github.io/blog/2013/06/29/basic-ssh-productivity-tips/"/>
<updated>2013-06-29T18:22:00+03:00</updated>
<id>http://Nazarii.github.io/blog/2013/06/29/basic-ssh-productivity-tips</id>
<content type="html"><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Secure_Shell">SSH</a> has many features which are helpful when working regularly with files on remote servers, together they can give a vast increase in productivity over the bare use of <a href="http://en.wikipedia.org/wiki/Secure_Shell">ssh</a>. If you regularly use <a href="http://en.wikipedia.org/wiki/Secure_Shell">ssh</a>, it’s worth spending a little time learning about these and configuring your environment to make your life easier.<!-- more -->
I’m working with ssh over a year and here are few tips to make this work easier when connecting to remote server.</p>
<h2>Too lazy to type password?</h2>
<p>If currently you type a password when making an SSH connection, you can make connecting much more pleasant by setting up SSH keys. With keys you do get prompted for a pass phrase, but this happens only once per booting your computer, rather than on every connection. With OpenSSH generate yourself a private key with:</p>
<figure class='code'><figcaption><span>zsh</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>ssh-keygen
</span></code></pre></td></tr></table></div></figure>
<p>and follow the prompts. Do provide a pass phrase, so your private key is encrypted on disk. Then you need to copy the public part of your key to servers you wish to connect to. If your system has ssh-copy-id then it’s as simple as:</p>
<figure class='code'><figcaption><span>zsh</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>ssh-copy-id [email protected]
</span></code></pre></td></tr></table></div></figure>
<p>Otherwise you need to do it manually:</p>
<p><em>1. Find the public key. The output of ssh-keygen should say where this is, probably ~/.ssh/id_rsa.pub.</em></p>
<p><em>2. On each of your remote servers insert the contents of that file into ~/.ssh/authorized_keys.</em></p>
<p><em>3. Make sure that only your user can write to both the directory and file.</em></p>
<p>Then you can SSH to servers, copy files, and commit code all without being hassled for passwords.</p>
<h2>Too lazy to type full hostnames?</h2>
<p>If you’re tired of typing same username and server address follow these tips:</p>
<h3>Hostname Aliases</h3>
<p>You can define hostname aliases in your SSH config, which is usually located in ~/.ssh/config though this can involve listing each hostname. For example:</p>
<figure class='code'><figcaption><span>vim</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='vim'><span class='line'>Host dev
</span><span class='line'> HostName dev.example.<span class="k">com</span>
</span><span class='line'> Port <span class="m">1422</span>
</span></code></pre></td></tr></table></div></figure>
<p>You can even use wildcards to group similar hostnames, using %h in the fully qualified domain name:</p>
<figure class='code'><figcaption><span>vim</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='vim'><span class='line'>Host dev intranet <span class="nb">backup</span>
</span><span class='line'> HostName %<span class="k">h</span>.example.<span class="k">com</span>
</span><span class='line'>
</span><span class='line'>Host dev mail
</span><span class='line'> HostName %<span class="k">h</span>.example.<span class="k">com</span>
</span></code></pre></td></tr></table></div></figure>
<p>Next time you type <strong>ssh dev</strong> it will connect to <strong>dev.example.com</strong>.</p>
<h3>Don’t Type Usernames</h3>
<p>If your username on a remote server is different from your local username, and when you often connect with the same user specify this in your SSH config as well:</p>
<figure class='code'><figcaption><span>vim</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='vim'><span class='line'>Host dev mail
</span><span class='line'> HostName %<span class="k">h</span>.example.<span class="k">com</span>
</span><span class='line'> <span class="nb">User</span> nazik
</span></code></pre></td></tr></table></div></figure>
<p>Now even though my local username is nazarii, I can just do:</p>
<figure class='code'><figcaption><span>zsh</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>ssh dev
</span></code></pre></td></tr></table></div></figure>
<p>and SSH will connect to the nazik account on the server.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Introducing ZSH.]]></title>
<link href="http://Nazarii.github.io/blog/2013/06/28/introducing-zsh-and-oh-my-zsh/"/>
<updated>2013-06-28T14:28:00+03:00</updated>
<id>http://Nazarii.github.io/blog/2013/06/28/introducing-zsh-and-oh-my-zsh</id>
<content type="html"><![CDATA[<p>Today I’d like to show you really usefull tool for working in command line I’ve discovered – <a href="http://www.zsh.org/">ZSH</a>.
The Z Shell (<a href="http://www.zsh.org/">zsh</a>) is a power shell that is not often used by many Linux users. The reason for this is that most Linux distributions install, and make default, the bash shell.<!--more--> zsh is packaged for virtually every Linux distribution and installation on Ubuntu is simple made by running</p>
<figure class='code'><figcaption><span>bash</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$sudo</span> apt-get install zsh
</span></code></pre></td></tr></table></div></figure>
<p>After installation main configuration file .zshrc is located in you’r home directory.
To make zsh as default shell run chsh command and include path to it’s executable:</p>
<figure class='code'><figcaption><span>bash</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>nazik@nazik-HP:~<span class="nv">$ </span>chsh
</span><span class='line'>Password:
</span><span class='line'>Changing the login shell <span class="k">for </span>nazik
</span><span class='line'>Enter the new value, or press ENTER <span class="k">for </span>the default
</span><span class='line'> Login Shell <span class="o">[</span>/bin/bash<span class="o">]</span>: /bin/zsh
</span></code></pre></td></tr></table></div></figure>
<p>One of the great features of zsh is tab-completion; it also handles all the logistics of tab-completion and is extremely easy to implement, just by adding two lines to your ~/.zshrc file:</p>
<figure class='code'><figcaption><span>zsh</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>autoload -U compinit
</span><span class='line'>compinit
</span></code></pre></td></tr></table></div></figure>
<p>The compinit function is what loads the tab-completion system by defining a shell function for every utility that zsh is able to tab-complete. By using autoload, you can optimize zsh by telling it to defer reading the definition of the function until it’s actually used, which speeds up the zsh startup time and reduces memory usage.</p>
<p>Also there is awesome plugin <a href="https://github.com/robbyrussell/oh-my-zsh">oh-my-zsh</a> especially if you are often working with <a href="http://en.wikipedia.org/wiki/Git_(software)">GIT</a> repositories.
To install this one run:</p>
<figure class='code'><figcaption><span>zsh</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$wget</span> --no-check-certificate https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh
</span></code></pre></td></tr></table></div></figure>
<p>After installation you might need to reload you shell:</p>
<figure class='code'><figcaption><span>zsh</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nb">source</span> ~/.zshrc
</span></code></pre></td></tr></table></div></figure>
<p>Each time you cd to you git repo it will show you branch you currently working on and whether you have uncommited local changes or not, if so it will show you gold sign:
<img class="center" src="http://Nazarii.github.io/images/zsh_screen.png" width="950" height="650" title="image" alt="images">
Also there are a lot of themes for this plugin you can choose to use the other one then default “robbyrussell” theme. List of themes you can get at this <a href="https://github.com/robbyrussell/oh-my-zsh/wiki/themes">link</a>. To change to other one you need to edit you .zshrc file and reload shell.</p>
<p>You can also use aliases after adding it to your config file.
To add time of command prompting to the right side of terminal append following line to your config file:</p>
<figure class='code'><figcaption><span>vim </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>RPROMPT=$'%{\e[1;34m%}%T%{\e[0m%}'</span></code></pre></td></tr></table></div></figure>
<p>.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[That's it! My first post.]]></title>
<link href="http://Nazarii.github.io/blog/2013/06/20/thats-it-my-first-post/"/>
<updated>2013-06-20T23:25:00+03:00</updated>
<id>http://Nazarii.github.io/blog/2013/06/20/thats-it-my-first-post</id>
<content type="html"><![CDATA[<p>So i decided to start my own blog with blackjack and hookers .<!-- more -->
Will try to write about technical Tips&Tricks that are interesting for me basicly in English (Please accept my apologies for my barbarian grammatic).
The main goal of this blog is to keep things I’m interested in in one place and to improve my English conversantional skills.</p>
<p>If you found something interesting – try this out!</p>
<p><img class="left" src="http://Nazarii.github.io/images/hookers.jpg" width="350" height="350" title="image" alt="images"></p>
]]></content>
</entry>
</feed>