Skip to main content

Home/ Python Programming/ Group items tagged functional

Rss Feed Group items tagged

dshekhar17

Python Inner Function / Nested Functions | Hack The Developer - 0 views

  •  
    Python Inner Function / Nested Functions
reckoner reckoner

Charming Python: Functional programming in Python, Part 1 - 0 views

  • Document options Document options requiring JavaScript are not displayed Rate this pageHelp us improve this contentLevel: IntroductoryDavid Mertz (mertz@gnosis.cx), Applied Metaphysician, Gnosis Software, Inc. 01 Mar 2001Although users usually think of Python as a procedural and object-oriented language, it actually contains everything you need for a completely functional approach to programming. This article discusses general concepts of functional programming, and illustrates ways of implementing functional techniques in Python. We'd better start with the hardest question: "What is functional programming (FP), anyway?" One answer would be to say that FP is what you do when you program in languages like Lisp, Scheme, Haskell, ML, OCAML, Clean, Mercury, or Erlang (or a few others). That is a safe answer, but not one that clarifies very much. Unfortunately, it is hard to get a consistent opinion on just what FP is, even from functional programmers themselves. A story about elephants and blind men seems apropos here. It is also safe to contrast FP with "imperative programming" (what you do in languages like C, Pascal, C++, Java, Perl, Awk, TCL, and most others, at least for the most part).
dshekhar17

Python Lambda Function/Anonymous Function | Hack The Developer - 0 views

  •  
    Python Lambda Function/Anonymous Function
reckoner reckoner

Python Idioms and Efficiency Suggestions - 0 views

  • What idioms should I use to make my code easier to read? Read "The Python Cookbook", especially the first few chapters. It's a great source of well-written Python code examples.
  • Use function factories to create utility functions. Often, especially if you're using map and filter a lot, you need utility functions that convert other functions or methods to taking a single parameter. In particular, you often want to bind some data to the function once, and then apply it repeatedly to different objects. In the above example, we needed a function that multiplied a particular field of an object by 3, but what we really want is a factory that's able to return for any field name and amount a multiplier function in that family:
  • Use zip and dict to map fields to names. zip turns a pair of sequences into a list of tuples containing the first, second, etc. values from each sequence. For example, zip('abc', [1,2,3]) == [('a',1),('b',2),('c',3)]. You can use this to save a lot of typing when you have fields in a known order that you want to map to names:
  •  
    suggestions for better programming style.
reckoner reckoner

Lightweight Approach to AOP (aspect-oriented programming) in Python - 0 views

  • aspects.py library provides means to intercept function calls. Functions and methods (also in Python standard library and third party code) can be wrapped so that when they are called, the wrap is invoked first. Depending on the wrap, the execution of the original function can be omitted, or the function can be called arbitrarily many times. Wraps are able to modify the arguments and the return value of the original function. In the terminology of aspect-oriented programming, the library allows applying advices (wraps) to call join points of methods and functions in around fashion.
  •  
    aspect-oriented programming
reckoner reckoner

6.5.1 Itertool functions - 0 views

  • 6.5.1 Itertool functions The following module functions all construct and return iterators. Some provide streams of infinite length, so they should only be accessed by functions or loops that truncate the stream.
  •  
    good examples
reckoner reckoner

Re: Python in Excel - 0 views

  • You can use Microsoft Script Control. If you have the win32 extensions of python, you can use python in place of vb in this control -open the VBA script editor - In menus/Tools/References add Microsoft Script Control -Make a new module and declare a new MsScriptControl.ScriptControl Global sc as new MsScriptControl.ScriptControl -Initialize the language attibute with python - Note that you and users of your document must have python and its win32 extensions installed. Activestate python distribustion include it. You can put sc.language="python" in the routine Workbook_Open() Now you can import python modules using ExecuteStatement method of the control in vba and have results from python functions with eval method. One interesting thing is that you can pass an object to the control with AddObject method and have python manipulate it. And so on..
  • Global sc As New MSScriptControl.ScriptControl Public Function os_getcwd() sc.Language = "python" sc.ExecuteStatement ("import os") os_getcwd = sc.Eval("os.getcwd()") End Function With this you can set your Excel formula to =os_getcwd() For me it returns "C:\Documents and Settings\Administrator\My Documents", which I needed to know at the time so I didn't have to screw around with the ever annoying pythonpath. You can put the first two lines of the function in the Workbook_Open hook, but I don't know where that is. I hope to use more Python in Excel soon. Hmm, actually, I suppose you can put those first two lines of the function after the Global declaration as well. I know just about zero VBScript and didn't get a chance to do anything else beyond proof of concept yet. I figured I would write something dynamic which allowed more transparent access to Python, maybe allowing formula like =py("os.getcwd()"), etc.
dshekhar17

https://hackthedeveloper.com/python-namedtuple-syntax-function/ - 0 views

  •  
    A namedtuple in python is a subclass of tuples. The named tuple has the same functionalities as a normal tuple, but its values can be accessed both by name (using dot notation eg: .name) as well as by position (offset notation eg: [0]).
reckoner reckoner

Python groupby, the iterator swiss army knife - Program - Python - Builder AU - 0 views

  • Last week we looked at the itertools module and how its iterator based functions can be faster than doing things from scratch. One of the examples showed a function using itertools.takewhile to be much faster than one using itertools.groupby. Some feedback we got, asked, what then is the point of groupby if it's just a slower version of other functionality?
reckoner reckoner

A Little Head Trauma...: Dynamic Functions - 0 views

  •  
    good discussion of *arguments in dynamic functions
anonymous

Python: Lambda Functions - 12 views

  •  
    lambda functions
reckoner reckoner

Applying sympy expressions on numpy arrays - sympy | Google Groups - 0 views

  • If I have:     from sympy import Symbol, Integrate     x = Symbol('x')     f = x**2 + x     g = Integrate(f, x) how can I apply g to a numpy array? In other words, how can I "numpify" the g expression, injecting in it x = numpy.linspace(1, 9, 9)? What would be even nicer would be to be able to retrieve a lambda using numpy functions for g as a function of x (that way I don't have the overhead of numpifying it each time I want to apply it to an array).
reckoner reckoner

g :: Dynamic Function Signatures - 0 views

  •  
    explains *args and **kwargs
reckoner reckoner

unit step (heaviside) function in sympy? - sympy | Google Groups - 0 views

  • On Mon, May 5, 2008 at 8:22 PM, Reckoner <recko...@gmail.com> wrote: > is there a unit step (heaviside) function in sympy? > I need to work a conditional into a symbolic expression. We have sign which is basically the same thing:
reckoner reckoner

Basic Threading in Python - Open Source Web Development Tutorials - 0 views

  • You can "turn an ordinary function into a thread" using the Thread class: just pass the function as the value for the `target' argument: import threading threading.Thread(target=your_function).start()
1 - 20 of 46 Next › Last »
Showing 20 items per page