473,804 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom behavior defined in the imported module

I have a new enhancement to pyparsing that doubles the parse speed (using a
technique called "packrat parsing"), but which is not suitable for all
parsers, specifically those that have complex parse actions. I don't want
to just enable this feature by default - I think there is too much risk of
it breaking existing code. Actually, I think the chance is <10%, but I
think I'm better off leaving the feature dormant unless explicitly enabled,
so that it doesn't take someone by surprise when they install the new
version.

The alternatives I've come up with for the user to enable this packrat parse
mode are:

1. Add a staticmethod enablePackrat() to the pyparsing ParserElement class,
to modify the ParserElement defintion of the internal (non-packrat) parse()
method. This method essentially runs code looking like:

ParserElement.p arse,ParserElem ent.originalPar se = \
ParserElement.p ackratParse,Par serElement.pars e

where the packratParse method is a memoizing wrapper around calls to
originalParse. This technique requires the caller to invoke
ParserElement.e nablePackrat() after having imported pyparsing.

This renaming trick actually breaks when using psyco, if psyco has compiled
the pyparsing methods before calling enablePackrat() . You have to import
pyparsing, call enablePackrat() , then call psyco.full(), or whatever psyco
commands to cache compiled versions of the pyparsing function code.

2. Implement some sort of "onImport" hook to allow the calling routine to
use a specialized import statement, something like "from pyparsing import
packratParsing" that would do the same as the above enablePackrat() call,
but perhaps in a more Pythonic-looking manner. (I was inspired along these
lines from Jim Hugunin's IronPython talk, in which he used import hooks to
modify the behavior of the base Python string class.) I've looked a bit at
imp, imputil, and ihooks, and these all seem to be modifiers to the import
mechanism as called from the importing module. What import hooks are
accessible from within the module being imported? Is there any way to see
which form of import was used, and to access the arguments of the import
command as invoked from the calling module?

I was hoping that by hooking into the import command, I could make this
mode-setting command more atomic with the actual import of pyparsing's class
and function definitions, to avoid problems like the psyco issue.
Plus I'm open to other suggestions as well.

Thanks,
-- Paul

Mar 25 '06 #1
4 1303
Paul McGuire wrote:
The alternatives I've come up with for the user to enable this packrat parse
mode are:

1. Add a staticmethod enablePackrat() to the pyparsing ParserElement class,
to modify the ParserElement defintion of the internal (non-packrat) parse()
method. This method essentially runs code looking like:

ParserElement.p arse,ParserElem ent.originalPar se = \
ParserElement.p ackratParse,Par serElement.pars e


Could you just define a module pyparsingpackra t like this:
from pyparsing import *
ParserElement.p arse,ParserElem ent.originalPar se = \
ParserElement.p ackratParse,Par serElement.pars e

Then users would just import pyparsingpackra t instead of pyparsing.

Kent
Mar 25 '06 #2
"Kent Johnson" <ke**@kentsjohn son.com> wrote in message
news:44******** **@newspeer2.td s.net...
Paul McGuire wrote:
The alternatives I've come up with for the user to enable this packrat parse mode are:

1. Add a staticmethod enablePackrat() to the pyparsing ParserElement class, to modify the ParserElement defintion of the internal (non-packrat) parse() method. This method essentially runs code looking like:

ParserElement.p arse,ParserElem ent.originalPar se = \
ParserElement.p ackratParse,Par serElement.pars e


Could you just define a module pyparsingpackra t like this:
from pyparsing import *
ParserElement.p arse,ParserElem ent.originalPar se = \
ParserElement.p ackratParse,Par serElement.pars e

Then users would just import pyparsingpackra t instead of pyparsing.

Kent


I *really* like keeping pyparsing's footprint down
Mar 25 '06 #3
"Kent Johnson" <ke**@kentsjohn son.com> wrote in message
news:44******** **@newspeer2.td s.net...
Paul McGuire wrote:
The alternatives I've come up with for the user to enable this packrat parse mode are:

1. Add a staticmethod enablePackrat() to the pyparsing ParserElement class, to modify the ParserElement defintion of the internal (non-packrat) parse() method. This method essentially runs code looking like:

ParserElement.p arse,ParserElem ent.originalPar se = \
ParserElement.p ackratParse,Par serElement.pars e


Could you just define a module pyparsingpackra t like this:
from pyparsing import *
ParserElement.p arse,ParserElem ent.originalPar se = \
ParserElement.p ackratParse,Par serElement.pars e

Then users would just import pyparsingpackra t instead of pyparsing.

Kent

(damned touchy touchpad!)

.... anyway...

I *really* like keeping pyparsing's footprint down to just one Python
module. It would be nice if I could pass an argument along with the import
statement:

import pyparsing(packr at=True)

or some such.

-- Paul
Mar 25 '06 #4
"Kent Johnson" <ke**@kentsjohn son.com> wrote in message
news:44******** **@newspeer2.td s.net...
Paul McGuire wrote:
The alternatives I've come up with for the user to enable this packrat parse mode are:

1. Add a staticmethod enablePackrat() to the pyparsing ParserElement class, to modify the ParserElement defintion of the internal (non-packrat) parse() method. This method essentially runs code looking like:

ParserElement.p arse,ParserElem ent.originalPar se = \
ParserElement.p ackratParse,Par serElement.pars e


Could you just define a module pyparsingpackra t like this:
from pyparsing import *
ParserElement.p arse,ParserElem ent.originalPar se = \
ParserElement.p ackratParse,Par serElement.pars e

Then users would just import pyparsingpackra t instead of pyparsing.

Kent


I'm also not overkeen on elevating the word "packrat" into the module name
itself!

-- Paul
Mar 25 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1041
by: paolo_veronelli | last post by:
Hello pyple, I have two modules :remote.py and local.py which have some classes with the same names specifically one we can call Model.So it exist remote.Model class and local.Model class A third module called uri.py derives some of its classes from a not better specified repository.Model at the beginning of uri.py I can write 'import local as repository' or
5
1673
by: Tommytrojan | last post by:
Hi, I have been using Python for a while but today I came across a really strange behavior: While poking around in Queue.py due to problems with importing this module from a thread I got an error that a module that I imported on top of the file could not be accessed. I reduced the problem to this small script:
19
4923
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the exception of custom Collection Classes. Background: I'm developing an A2K .mdb to be deployed as an .mde at my current job-site. It has several custom controls which utilize custom classes to wrap built-in controls, and add additional functionality....
15
6541
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt accept other controls. The control i drag drop on it becomes the child of my custom control's parent form and not the child of my custom control. Then i added this line "" before my custom control class (i dont know what this line does). Now
1
1878
by: Kevin Walzer | last post by:
I'm trying to create a custom Tkinter widget class, and I'm having some difficulty getting it set up properly. The class is called MacToolbar, saved in its own module MacToolbar.py, and imported with this statement: import MacToolbar Here is the relevant portion of the class:
11
2584
by: Nadeem | last post by:
Hello all, I'm trying to write a function that will dynamically generate other functions via exec. I then want to be able to import the file (module) containing this function and use it in other modules, but for some reason it only works using the "import <mod>" syntax, and not "from <modimport *" syntax... i.e. in the latter case, the function is dynamically generated, but not accessible from the importing module. Any ideas on what I can...
2
19499
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
3
1831
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and imports ) Take Example :- in A.py :- import B print dir() # no problems we can see B which contain re module and C module
0
2897
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10351
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6866
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.