473,725 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pure python code to do modular-arithmetic unit conversions?


Is there already a pure python module that can do modular-arithmetic unit
conversions, like converting a huge number of seconds into months,
weeks... or a bandwidth measure into megabits/s or gigabits/s or
megabytes/s or gigabytes/s, whatever's the most useful (ala df -h)?

Thanks!

Jul 18 '05 #1
2 3641
Dan Stromberg wrote:
Is there already a pure python module that can do modular-arithmetic unit conversions, like converting a huge number of seconds into months,
weeks...
Use the divmod function.
SECONDS_PER_MON TH = 2629746 # 1/4800 of 400 Gregorian years

def convert_seconds (seconds):
"Return (months, weeks, days, hours, minutes, seconds)."
months, seconds = divmod(seconds, SECONDS_PER_MON TH)
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
weeks, days = divmod(days, 7)
return months, weeks, days, hours, minutes, seconds

def to_seconds(mont hs, weeks, days, hours, minutes, seconds):
return (((weeks * 7 + days) * 24 + hours) * 60 + minutes) * 60 + \
seconds + months * SECONDS_PER_MON TH
convert_seconds (10**9) (380, 1, 1, 1, 28, 40)
to_seconds(*_) 1000000000
or a bandwidth measure into megabits/s or gigabits/s or
megabytes/s or gigabytes/s, whatever's the most useful (ala df -h)?


def convert_bytes(b ytes):
PREFIXES = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
x = bytes
for prefix in PREFIXES:
if x < 1024:
return '%.4g %sB' % (x, prefix)
x /= 1024.
# No SI prefixes left, so revert to scientific notation
return '%.3e B' % bytes convert_bytes(4 0e9) '37.25 GB' convert_bytes(2 048)

'2 KB'

Jul 18 '05 #2
Dan Stromberg wrote:
Is there already a pure python module that can do modular-arithmetic unit
conversions, like converting a huge number of seconds into months,
weeks... or a bandwidth measure into megabits/s or gigabits/s or
megabytes/s or gigabytes/s, whatever's the most useful (ala df -h)?

Thanks!

Take a look at:
http://home.tiscali.be/be052320/Unum_tutorial.html

From the intro:

"Unum stands for 'unit-numbers'. It is a Python module that allows to define and
manipulate true quantities, i.e. numbers with units such as 60 seconds, 500
watts, 42 miles-per-hour, 100 kg per square meter, 14400 bits per second, 30
dollars etc. The module validates unit consistency in arithmetic expressions; it
provides also automatic conversion and output formatting."

Michael

Jul 18 '05 #3

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

Similar topics

11
1963
by: Brett C. | last post by:
For my thesis (once the bloody thing stops throwing bugs at me) I am going to need to collect stats on the frequency that atomic types in local variables are applied to various opcodes and methods. That means that I need something to collect stats off of. So I am trying to come up with a list of projects I can use to get my stats from. the stdlib is a no-brainer. Also plan to use Docutils. But beyond those most of the big Python...
15
2210
by: Christos TZOTZIOY Georgiou | last post by:
Has anyone built PIL (1.1.4 or 1.1.5) for Python 2.4? If yes, please let me know, since I can't test a couple of my apps using PIL with 2.4 . You can even reply by email (yes, I dare use true email in newsgroup postings, but then, I'm postmaster and welcome spam :) TIA -- TZOTZIOY, I speak England very best, "Tssss!" --Brad Pitt as Achilles in unprecedented Ancient Greek
5
2693
by: Fuzzyman | last post by:
Python 2.4 is built with Microsoft Visiual C++ 7. This means that it uses msvcr7.dll, which *isn't* a standard part of the windows operating system. This means that if you build a windows installer using distutils - it *requires* msvcr7.dll in order to run. This is true even if your package is a pure python package. This means that when someone tries to use a windows installer created with Python 2.4, on a machine with only python 2.3 - it...
2
1393
by: Ken Guest | last post by:
Hi, I've two relatively small web applications that are currently implemented in PHP and needed to be migrated to python and most likely zope afterwards as we're getting a third-party Zope powered CMS later this year. There isn't an immediate need for them to be developed as zope modules/extensions so I was wondering what the best approach might me - especially as they'd be deployed in the middle of a PHP driven website. Would I be...
13
2159
by: Steven Bethard | last post by:
Jean-Paul Calderone <exarkun@divmod.comwrote: Interesting. Could you give a few illustrations of this? (I didn't run into the same problem at all, so I'm curious.) Steve
17
8095
by: DanielJohnson | last post by:
how to use the combination function in python ? For example 9 choose 2 (written as 9C2) = 9!/7!*2!=36 Please help, I couldnt find the function through help.
206
8344
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of the computer language Python it is claimed: \There should be one|and preferably only one|obvious...
12
3141
by: betabrain.honshu | last post by:
Hi Folks, for those of you who are familiar with the micropledge.com project, here is a good opportunity to spend or earn something: http://micropledge.com/projects/pysalsa20 I know that the details of this project are still a bit unclear, but that is something we could discuss. By the way, the end result should look like it's a part of the python standard library (naming conventions, etc.) and of course it will be open source.
28
2453
by: n00m | last post by:
Both codes below read the same huge(~35MB) text file. In the file 1000000 lines, the length of each line < 99 chars. Stable result: Python runs ~0.65s C : ~0.70s Any thoughts?
7
1798
by: Anthony | last post by:
Hi, I'm a FoxPro programmer, but I want to learn python before it's too late. I do a lot of statistical programming, so I import SPSS into python. In my opinion, the best features of Visual FoxPro 9.0 were: a) Intellisense (tells you what classes/methods are available and what variables go into a function) b) Code Completion (guesses your code after four letters) c) Data-Orientation; multiple data sessions can be open, data can be...
0
8888
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...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6011
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
4519
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
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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
3
2157
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.