sys

Description: Программирование на супер модном мега крутом языке Питон.

dyvniy M
Topic author, Администратор
Администратор
Avatar
dyvniy M
Topic author, Администратор
Администратор
Age: 39
Reputation: 1
Loyalty: 1
Posts: 3472
Joined: Wed, 10 Oct 2012
With us: 9 years 3 months
Профессия: Программист
Location: Россия, Москва
ICQ Website Skype VK

#13by dyvniy » Tue, 7 Mar 2017, 16:46:10

*args, **kwargs
http://stackoverflow.com/questions/3394835/args-and-kwargs
Spoiler
You would use *args when you're not sure how many arguments might be passed to your function, i.e. it allows you pass an arbitrary number of arguments to your function. For example:

Code: Select all

>>> def print_everything(*args):
...     for count, thing in enumerate(args):
...         print '{0}. {1}'.format(count, thing)
...
>>> print_everything('apple', 'banana', 'cabbage')
0. apple
1. banana
2. cabbage


Similarly, **kwargs allows you to handle named arguments that you have not defined in advance:

Code: Select all

>>> def table_things(**kwargs):
...     for name, value in kwargs.items():
...         print '{0} = {1}'.format(name, value)
...
>>> table_things(apple = 'fruit', cabbage = 'vegetable')
cabbage = vegetable
apple = fruit
Image

dyvniy M
Topic author, Администратор
Администратор
Avatar
dyvniy M
Topic author, Администратор
Администратор
Age: 39
Reputation: 1
Loyalty: 1
Posts: 3472
Joined: Wed, 10 Oct 2012
With us: 9 years 3 months
Профессия: Программист
Location: Россия, Москва
ICQ Website Skype VK

#14by dyvniy » Wed, 19 Apr 2017, 09:31:20

How to crash python
https://wiki.python.org/moin/CrashingPython
ctypes

Code: Select all

def crash():
        '''\
        crash the Python interpreter...
        '''
        i = ctypes.c_char('a')
        j = ctypes.pointer(i)
        c = 0
        while True:
                j[c] = 'a'
                c += 1
        j
Image

dyvniy M
Topic author, Администратор
Администратор
Avatar
dyvniy M
Topic author, Администратор
Администратор
Age: 39
Reputation: 1
Loyalty: 1
Posts: 3472
Joined: Wed, 10 Oct 2012
With us: 9 years 3 months
Профессия: Программист
Location: Россия, Москва
ICQ Website Skype VK

#15by dyvniy » Thu, 22 Jun 2017, 11:39:20

Чтобы сервис (pythonservice) запускался питоновская dll должна быть в path.
http://pythonistaa.blogspot.ru/2011/06/pywin32.html
Image

dyvniy M
Topic author, Администратор
Администратор
Avatar
dyvniy M
Topic author, Администратор
Администратор
Age: 39
Reputation: 1
Loyalty: 1
Posts: 3472
Joined: Wed, 10 Oct 2012
With us: 9 years 3 months
Профессия: Программист
Location: Россия, Москва
ICQ Website Skype VK

#16by dyvniy » Thu, 31 Aug 2017, 14:54:07

Example of service in Python3

Code: Select all

import pythoncom
import win32serviceutil
import win32service
import win32event
import servicemanager
import requests
import socket
import time
import sys
import os

class TestService(win32serviceutil.ServiceFramework):
    _svc_name_ = 'Cloud2CloudService_test'
    _svc_display_name_ = 'Cloud to Cloud Service_test'
   
    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)       
        socket.setdefaulttimeout(60)
       
    def SvcStop(self):
        #DBG.info('STOPPING_SERVICE')
        config.need_stop_service = True
        #sendNop()
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)
        #DBG.info('SERVICE_WAS_STOPPED')
       
    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, ''))
        #DBG.info('STARTING_SERVICE')
        #config.need_stop_service = False
        #config.run_as_service = True
        self.main()
   
    def main(self):
        time.sleep(10)
        #main()

if __name__ == '__main__':
    if len(sys.argv) < 2:
        sys.argv.append('install')
    win32serviceutil.HandleCommandLine(TestService)
Image

dyvniy M
Topic author, Администратор
Администратор
Avatar
dyvniy M
Topic author, Администратор
Администратор
Age: 39
Reputation: 1
Loyalty: 1
Posts: 3472
Joined: Wed, 10 Oct 2012
With us: 9 years 3 months
Профессия: Программист
Location: Россия, Москва
ICQ Website Skype VK

#17by dyvniy » Fri, 8 Sep 2017, 14:35:34

Зачем нужны лямбды
https://stackoverflow.com/questions/890128/why-are-python-lambdas-useful?rq=1
Spoiler
Are you talking about lambda functions? Like

lambda x: x**2 + 2*x - 5
Those things are actually quite useful. Python supports a style of programming called functional programming where you can pass functions to other functions to do stuff. Example:

mult3 = filter(lambda x: x % 3 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9])
sets mult3 to [3, 6, 9], those elements of the original list that are multiples of 3. This is shorter (and, one could argue, clearer) than

def filterfunc(x):
return x % 3 == 0
mult3 = filter(filterfunc, [1, 2, 3, 4, 5, 6, 7, 8, 9])
Of course, in this particular case, you could do the same thing as a list comprehension:

mult3 = [x for x in [1, 2, 3, 4, 5, 6, 7, 8, 9] if x % 3 == 0]
(or even as range(3,10,3)), but there are many other, more sophisticated use cases where you can't use a list comprehension and a lambda function may be the shortest way to write something out.

Returning a function from another function

>>> def transform(n):
... return lambda x: x + n
...
>>> f = transform(3)
>>> f(4)
7
This is often used to create function wrappers, such as Python's decorators.
Combining elements of an iterable sequence with reduce()
>>> reduce(lambda a, b: '{}, {}'.format(a, b), [1, 2, 3, 4, 5, 6, 7, 8, 9])
'1, 2, 3, 4, 5, 6, 7, 8, 9'
Sorting by an alternate key
>>> sorted([1, 2, 3, 4, 5, 6, 7, 8, 9], key=lambda x: abs(5-x))
[5, 4, 6, 3, 7, 2, 8, 1, 9]
I use lambda functions on a regular basis. It took me a while to get used to them, but eventually I came to understand that they're a very valuable part of the language.
Image

dyvniy M
Topic author, Администратор
Администратор
Avatar
dyvniy M
Topic author, Администратор
Администратор
Age: 39
Reputation: 1
Loyalty: 1
Posts: 3472
Joined: Wed, 10 Oct 2012
With us: 9 years 3 months
Профессия: Программист
Location: Россия, Москва
ICQ Website Skype VK

#18by dyvniy » Thu, 8 Feb 2018, 18:15:20

Attaches to a running python program and generates profiling data for FlameGraphs

https://eng.uber.com/pyflame/
https://github.com/uber/pyflame
https://github.com/brendangregg/FlameGraph
Image


Forum name: Python
Description: Программирование на супер модном мега крутом языке Питон.

Quick reply


Enter the code exactly as it appears. All letters are case insensitive.
Confirmation code
:) ;) :hihi: :P :hah: :haha: :angel: :( :st: :_( :cool: 8-| :beee: :ham: :rrr: :grr: :* :secret: :stupid: :music: View more smilies
   

Return to “Python”

Who is online (over the past 15 minutes)

Users browsing this forum: 1 guest