sys

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

dyvniy M
Автор темы, Администратор
Администратор
Аватара
dyvniy M
Автор темы, Администратор
Администратор
Возраст: 41
Репутация: 1
Лояльность: 1
Сообщения: 3579
Зарегистрирован: Ср, 10 октября 2012
С нами: 11 лет 5 месяцев
Профессия: Программист
Откуда: Россия, Москва
ICQ Сайт Skype ВКонтакте

#13 dyvniy » Вт, 7 марта 2017, 16:46:10

*args, **kwargs
http://stackoverflow.com/questions/3394835/args-and-kwargs
Спойлер
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:

Код: Выделить всё

>>> 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:

Код: Выделить всё

>>> 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
Изображение

dyvniy M
Автор темы, Администратор
Администратор
Аватара
dyvniy M
Автор темы, Администратор
Администратор
Возраст: 41
Репутация: 1
Лояльность: 1
Сообщения: 3579
Зарегистрирован: Ср, 10 октября 2012
С нами: 11 лет 5 месяцев
Профессия: Программист
Откуда: Россия, Москва
ICQ Сайт Skype ВКонтакте

#14 dyvniy » Ср, 19 апреля 2017, 09:31:20

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

Код: Выделить всё

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
Изображение

dyvniy M
Автор темы, Администратор
Администратор
Аватара
dyvniy M
Автор темы, Администратор
Администратор
Возраст: 41
Репутация: 1
Лояльность: 1
Сообщения: 3579
Зарегистрирован: Ср, 10 октября 2012
С нами: 11 лет 5 месяцев
Профессия: Программист
Откуда: Россия, Москва
ICQ Сайт Skype ВКонтакте

#15 dyvniy » Чт, 22 июня 2017, 11:39:20

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

dyvniy M
Автор темы, Администратор
Администратор
Аватара
dyvniy M
Автор темы, Администратор
Администратор
Возраст: 41
Репутация: 1
Лояльность: 1
Сообщения: 3579
Зарегистрирован: Ср, 10 октября 2012
С нами: 11 лет 5 месяцев
Профессия: Программист
Откуда: Россия, Москва
ICQ Сайт Skype ВКонтакте

#16 dyvniy » Чт, 31 августа 2017, 14:54:07

Example of service in Python3

Код: Выделить всё

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)
Изображение

dyvniy M
Автор темы, Администратор
Администратор
Аватара
dyvniy M
Автор темы, Администратор
Администратор
Возраст: 41
Репутация: 1
Лояльность: 1
Сообщения: 3579
Зарегистрирован: Ср, 10 октября 2012
С нами: 11 лет 5 месяцев
Профессия: Программист
Откуда: Россия, Москва
ICQ Сайт Skype ВКонтакте

#17 dyvniy » Пт, 8 сентября 2017, 14:35:34

Зачем нужны лямбды
https://stackoverflow.com/questions/890128/why-are-python-lambdas-useful?rq=1
Спойлер
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.
Изображение

dyvniy M
Автор темы, Администратор
Администратор
Аватара
dyvniy M
Автор темы, Администратор
Администратор
Возраст: 41
Репутация: 1
Лояльность: 1
Сообщения: 3579
Зарегистрирован: Ср, 10 октября 2012
С нами: 11 лет 5 месяцев
Профессия: Программист
Откуда: Россия, Москва
ICQ Сайт Skype ВКонтакте

#18 dyvniy » Чт, 8 февраля 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
Изображение


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

Быстрый ответ


Введите код в точности так, как вы его видите. Регистр символов не имеет значения.
Код подтверждения
:) ;) :hihi: :P :hah: :haha: :angel: :( :st: :_( :cool: 8-| :beee: :ham: :rrr: :grr: :* :secret: :stupid: :music: Ещё смайлики…
   

Вернуться в «Python»

Кто сейчас на форуме (по активности за 15 минут)

Сейчас этот раздел просматривают: 7 гостей
Боты: Google [Bot]