473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Question by someone coming from C...

Writing this app in Python, not sure what the "best practice" would
be.

I want a bitfield global logging level that allows me to turn specific
debugging modules on and off. If I was doing this in C, I'd just use
some globals like:

unsigned int debug_level = 0;
#define DEBUG_GENERAL 0x0001
#define DEBUG_CONFIG 0x0002
#define DEBUG_OPTIONS 0x0004
etc etc

So I guess my questions are:

1. there doesn't seem to be a way to define global constants like in
other languages?
2. any special voodoo to use bitfields in Python?

Thanks!
Skye

Jun 27 '08 #1
12 856
Lie
On Jun 10, 4:00*am, Skye <spo...@gmail.comwrote:
Writing this app in Python, not sure what the "best practice" would
be.

I want a bitfield global logging level that allows me to turn specific
debugging modules on and off. *If I was doing this in C, I'd just use
some globals like:

unsigned int debug_level = 0;
#define DEBUG_GENERAL 0x0001
#define DEBUG_CONFIG 0x0002
#define DEBUG_OPTIONS 0x0004
etc etc

So I guess my questions are:

1. there doesn't seem to be a way to define global constants like in
other languages?
Global variables is like this:

-- module.py --
someglobal = 1
def func():
print someglobal
# This makes someglobal readonly,
# any attempt to write to someglobal
# would create a new local variable.

def func2():
global someglobal
someglobal = 2
# this allows you to manipulate the global
# variable
-- --
2. any *special voodoo to use bitfields in Python?
That's a not a good idea in Python. In C, it might worth saving some
few bytes of memory especially for embedded application, in python
it's just not worth it, the virtual machine is way huge compared to
the few bytes of space saved.

OK, it is possible to do bitfield manipulation in python:
http://aspn.activestate.com/ASPN/Coo.../Recipe/113799 but
it's not really worth it
>
Thanks!
Skye
Jun 27 '08 #2
OK, sounds good. So if not bitfields, what would be a good Python-y
way to do it?
Flip booleans in a "debug config" dictionary or something?

Skye

Jun 27 '08 #3
On Jun 9, 2:00 pm, Skye <spo...@gmail.comwrote:
Writing this app in Python, not sure what the "best practice" would
be.

I want a bitfield global logging level that allows me to turn specific
debugging modules on and off. If I was doing this in C, I'd just use
some globals like:

unsigned int debug_level = 0;
#define DEBUG_GENERAL 0x0001
#define DEBUG_CONFIG 0x0002
#define DEBUG_OPTIONS 0x0004
etc etc

So I guess my questions are:

1. there doesn't seem to be a way to define global constants like in
other languages?
2. any special voodoo to use bitfields in Python?

Thanks!
Skye
There is no conventional way to define a constant. There may be some
tricks if you look around, but I've been coding Python for a long time
and never actually needed one. The side effect is that you could
redefine one of the values at run-time. By naming convention though,
the user will know not to do that, just like they know not to mess
with attributes of an object that begin with "_". If they do mess with
it, well, they better know what they are doing.

There is no special bitfield voodoo that I know of.

Generally, naming conventions are the same as they would be in C (all
caps):

FOO = 0x01
BAR = 0x02
BAZ = 0x04

val = FOO | BAR | BAZ

if val & BAR:
#do bar stuff

The only time to do that sort of thing (in python) is when interacting
with something else that isn't written in Python though. In general,
for logging, just use the standard logging module:
http://docs.python.org/lib/module-logging.html
Matt
Jun 27 '08 #4
Lie
On Jun 10, 4:32*am, Skye <spo...@gmail.comwrote:
OK, sounds good. *So if not bitfields, what would be a good Python-y
way to do it?
The word is "pythonic".
Flip booleans in a "debug config" dictionary or something?
I'm not really sure, I've been programming with Python (and some other
languages) but never really felt the need to store data in such super-
efficient manners. I reckon a dict should do the job and it would be
easier to read too compared to bitfield, since dict's entry is named.

Actually, in python I usually do not put debugging codes before
entering debugging cycle, and I usually remove all those debugging
codes when the debugging cycle has finished (I'm talking for myself,
not the whole python community).
Skye
Jun 27 '08 #5
On Jun 9, 2:35*pm, Matimus <mccre...@gmail.comwrote:
The only time to do that sort of thing (in python) is when interacting
with something else that isn't written in Python though. In general,
for logging, just use the standard logging module:http://docs.python.org/lib/module-logging.html
Thanks! It looks like subclassing the logging module would be a much
better idea :)

Skye

Jun 27 '08 #6
Skye schrieb:
Writing this app in Python, not sure what the "best practice" would
be.

I want a bitfield global logging level that allows me to turn specific
debugging modules on and off. If I was doing this in C, I'd just use
some globals like:

unsigned int debug_level = 0;
#define DEBUG_GENERAL 0x0001
#define DEBUG_CONFIG 0x0002
#define DEBUG_OPTIONS 0x0004
etc etc

For debugging - use the module logging. And configure properly.

Diez
Jun 27 '08 #7
On Mon, 2008-06-09 at 15:02 -0700, Skye wrote:
On Jun 9, 2:35 pm, Matimus <mccre...@gmail.comwrote:
The only time to do that sort of thing (in python) is when interacting
with something else that isn't written in Python though. In general,
for logging, just use the standard logging module:http://docs.python.org/lib/module-logging.html

Thanks! It looks like subclassing the logging module would be a much
better idea :)

Skye

--
http://mail.python.org/mailman/listinfo/python-list
Really, the logging module is heavy weight enough, it's very doubtful
you need to add any functionality at all.

If you look at the documentation for logger objects, you'll see that you
can use a number of them, and set different logging levels for each one.
So, to get equivalent functionality to your bitfields, instead make a
separate logger for each category (config, options, blah...) and call
the object to log for that type.

i.e. config.debug( ... ), options.error( ... )

Since you probably want access to these from many different places in
your code, I find the simplest way is to create a logging module of your
own (not called logging, obviously) and instantiate all of your loggers
in that namespace, then import that one module as needed.

Perhpas you want one logging message to qualify as several different
types? That's the only place where I could see this being too verbose,
but even then it'd be easy to make a wrapper function that made that
easy given a collection of loggers.
--
John Krukoff <jk******@ltgc.com>
Land Title Guarantee Company

Jun 27 '08 #8
On Jun 9, 5:00 pm, Skye <spo...@gmail.comwrote:
Writing this app in Python, not sure what the "best practice" would
be.

I want a bitfield global logging level that allows me to turn specific
debugging modules on and off. If I was doing this in C, I'd just use
some globals like:

unsigned int debug_level = 0;
#define DEBUG_GENERAL 0x0001
#define DEBUG_CONFIG 0x0002
#define DEBUG_OPTIONS 0x0004
etc etc

So I guess my questions are:

1. there doesn't seem to be a way to define global constants like in
other languages?
2. any special voodoo to use bitfields in Python?
Apart from the good advice "use the logging module", here is the
Pythonic way you'd do this sort of thing in general. (There's not
always a spiffy built-in module for anything you want to do; just
usually. :)

The lack of globals is a minor issue; you can get globally accessible
values by storing them in a module and importing that module.

The way I'd do the above is to define a module, say config.py, to hold
configuration options. Then I'd define each condition in its own
variable:

debug_general = False
debug_config = False
debug_options = False

I could then enable debugging by changing the value:

import config
config.debug_general = True

And I could use print debugging output based on the config settings
like this:

import config
if config.debug_general or config.debug_options:
print_debugging_info()

But again, the logging modules handles all this for you so no point
for this particular task.

P.S. I'd do it more or less this way in C, too.
Carl Banks
Jun 27 '08 #9
Very cool - I'm liking the pythonic way of doing things more and
more.
The logger namespace/singleton idea makes great sense!

I see how the module variables make globals irrelevant, thanks!

Skye

Jun 27 '08 #10
Skye a écrit :
Writing this app in Python, not sure what the "best practice" would
be.

I want a bitfield global logging level that allows me to turn specific
debugging modules on and off. If I was doing this in C, I'd just use
some globals like:

unsigned int debug_level = 0;
#define DEBUG_GENERAL 0x0001
#define DEBUG_CONFIG 0x0002
#define DEBUG_OPTIONS 0x0004
etc etc

So I guess my questions are:

1. there doesn't seem to be a way to define global constants like in
other languages?
2. any special voodoo to use bitfields in Python?
Others already gave you the best advises (namely: using the logging
module). But let's answer your questions anyway:

1/ by convention, anything named ALL_UPPER is considered a constant.
Anyone breaking your code by messing with a PSEUDO_CONSTANT is on it's
own and has no right to complain. Consider it as a "warranty void if
unsealed" warning.
2/ just use plain integers and bitwise operators. But we're usually more
concerned about readability than about saving bits, and I've rarely
(maybe twice ?) seen bitfields used that way in Python.

Jun 27 '08 #11
John Krukoff wrote:
Since you probably want access to these from many different places in
your code, I find the simplest way is to create a logging module of your
own (not called logging, obviously) and instantiate all of your loggers
in that namespace, then import that one module as needed.
No, don't do that. Simple do

import logging
log = logging.getLogger("some_name")

The logging module takes care of the rest. The logging.getLogger()
function creates a new logger *only* when the name hasn't been used yet.

Christian

Jun 27 '08 #12

On Wed, 2008-06-11 at 00:43 +0200, Christian Heimes wrote:
John Krukoff wrote:
Since you probably want access to these from many different places in
your code, I find the simplest way is to create a logging module of your
own (not called logging, obviously) and instantiate all of your loggers
in that namespace, then import that one module as needed.

No, don't do that. Simple do

import logging
log = logging.getLogger("some_name")

The logging module takes care of the rest. The logging.getLogger()
function creates a new logger *only* when the name hasn't been used yet.

Christian

--
http://mail.python.org/mailman/listinfo/python-list
Nifty, I never noticed that function, thanks for pointing it out as
it'll make my application a bit simpler.

Now, if they'd only add a syslog module that uses the libc syslog
interface (sure, it wouldn't be thread safe, but at least it'd be
portable to AIX, unlike the current one), I'll be a happy camper.
--
John Krukoff <jk******@ltgc.com>
Land Title Guarantee Company

Jun 27 '08 #13

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

Similar topics

17
by: ZirC°N | last post by:
Hi, I am running NOF on a Win98se machine. The fonts in the Properties dialogs are very difficult to read being bolded and not complete (pixels). Has anyone seen this bug before? Is NOF suitable...
1
by: gregcd | last post by:
My client wants me to make a popup on exit window with a discount coupon. I have found how to do this if someone exits the site or closes their browser window, but I can't find a way to prevent...
7
by: Trev | last post by:
Hey, I just want to know (i know i should know this) how can i show up another form? I have 2 forms and the first one has a button on it that when i click the button i want the second form to...
6
by: Zach | last post by:
Hi everyone, I'm a bit of a newbie to C# programming so forgive this innocent question, but coming from a C++ background this seems very odd to me, and I'm hoping someone can shed some light...
29
by: Knut Olsen-Solberg | last post by:
I try to change the text in a <p> using getElementById(). I wonder what properties exists, and which one to use here. (The following does not work.) Regards Knut ______________________ ...
5
by: archana | last post by:
Hi all, I am using timer to do some functionality on user specified time. I am using system.timers.timer class and its timer to do this functionality. What i am doing is i set autoreset to...
9
by: David Eades | last post by:
Hi all Complete newbie here, so apologies if this is the wrong forum. I've been asked to use mysql and asp to make a simple bidding system (rather like a simple ebay), whereby users can use a...
28
by: Joe Reynolds | last post by:
this is kinda off topic, but not totally. i have an ASP page that pulls text from a database, stores it in a variable, then uses <%= variable %to write the text inside an html table. the problem...
11
by: RoB | last post by:
Hi all, I'm coming from the Informix world and I have a customer using DB2 8.2.3 for Linux on Red Hat Enterprise ES. The customer is performing filesystem backups of the containers etc every...
6
by: damiensawyer | last post by:
Hi, Can someone please explain to me something about delegates? My understanding is as follows. A delegate is basically an object that can hold a reference to a "method" somewhere. That is,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.