Most viewed recipes tagged "parse"http://code.activestate.com/recipes/tags/parse/views/2013-05-27T22:02:07-07:00ActiveState Code Recipesparse a date/time string to a `datetime` instance (Python)
2010-04-02T07:32:17-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577135-parse-a-datetime-string-to-a-datetime-instance/
<p style="color: grey">
Python
recipe 577135
by <a href="/recipes/users/4173505/">Trent Mick</a>
(<a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/time/">time</a>).
Revision 2.
</p>
<pre class="prettyprint"><code>>>> import datetime
>>> str(datetime.datetime.now())
'2010-03-21 21:33:32.750246'
>>> str(datetime.date.today())
'2010-03-21'
</code></pre>
<p>This function goes the other way for date and datetime strings of this format.</p>
Convert datetime in python to user friendly representation. (Python)
2009-08-15T01:00:03-07:00Jai Vikram Singh Vermahttp://code.activestate.com/recipes/users/4171450/http://code.activestate.com/recipes/576880-convert-datetime-in-python-to-user-friendly-repres/
<p style="color: grey">
Python
recipe 576880
by <a href="/recipes/users/4171450/">Jai Vikram Singh Verma</a>
(<a href="/recipes/tags/ago/">ago</a>, <a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/representation/">representation</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/user_friendly/">user_friendly</a>).
</p>
<p>A small contribution to the developer community.</p>
<p>This module caters to the need of developers who want to put date & time of post in terms like <br />
"X days, Y hrs ago", "A hours B mins ago", etc. in their applications rather then a basic timestamp <br />
like "2009-08-15 03:03:00". Additionally it also <br />
provides since epoch for a given datetime. </p>
<p>It takes in a Python datetime object as an input <br />
and provides a fancy datetime (as I call it) and <br />
the seconds since epoch. </p>
Python code clone detector (Don't Repeat Yourself) (Python)
2012-07-12T14:59:11-07:00frahttp://code.activestate.com/recipes/users/4182629/http://code.activestate.com/recipes/578206-python-code-clone-detector-dont-repeat-yourself/
<p style="color: grey">
Python
recipe 578206
by <a href="/recipes/users/4182629/">fra</a>
(<a href="/recipes/tags/analysis/">analysis</a>, <a href="/recipes/tags/clone/">clone</a>, <a href="/recipes/tags/code/">code</a>, <a href="/recipes/tags/dry/">dry</a>, <a href="/recipes/tags/duplication/">duplication</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/similarity/">similarity</a>, <a href="/recipes/tags/static/">static</a>, <a href="/recipes/tags/syntax/">syntax</a>).
Revision 2.
</p>
<p>Find duplicate code in Python 2/3 source files. Write a nice report about it.</p>
<p>Works at the Abstract Syntax Tree level, which is a robust way to detect clones.
See this <a href="http://francois.boutines.free.fr/python-3.2-report.html">code duplicated in the Python 3.2 standard library</a>.</p>
<p><strong>Update</strong>: I cleaned the code a little bit, made it Python 2.7 compatible and faster.</p>
parse png image (Python)
2013-05-27T22:02:07-07:00judyhttp://code.activestate.com/recipes/users/4186659/http://code.activestate.com/recipes/578534-parse-png-image/
<p style="color: grey">
Python
recipe 578534
by <a href="/recipes/users/4186659/">judy</a>
(<a href="/recipes/tags/height/">height</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/png/">png</a>, <a href="/recipes/tags/width/">width</a>).
</p>
<p>parse png image find all chunks width height</p>
Simple regex engine, elementary Python (Python)
2010-07-10T10:43:30-07:00Joost Behrendshttp://code.activestate.com/recipes/users/4174081/http://code.activestate.com/recipes/577251-simple-regex-engine-elementary-python/
<p style="color: grey">
Python
recipe 577251
by <a href="/recipes/users/4174081/">Joost Behrends</a>
(<a href="/recipes/tags/cached/">cached</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/parsing/">parsing</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/regular_expressions/">regular_expressions</a>).
Revision 40.
</p>
<p>A short engine for testing against a regex, understanding the 3 common quantifiers
?,+,* (non-greedy) working on characters, ., [...], [^...], \s, \S, bracketed patterns and group designators \N. Accepts unicode objects and fixed-width encoded strings
(but problems with eventual comparisons of trailing bytes in multi-byte utf-letters).
Captures up to 10 groups ( (?:...) implemented), which can be used for back referencing and in xreplace(). Captured groups are accessible after the search in the global list xGroups. | is supported, but only in groups and needing nested=True. nested=False is making '(' and ')' common letters.</p>
<p>This is not about Python or for Python, there it has little use beside re. But regarding that re needs about 6,000 lines you might agree with the author, that these 176 lines are powerful. This was the reason to publish it as a recipe - as a kind of (fairly complete) minimal example of a regex tester and as an example for corresponding recursive structures in data (TokenListCache) and code.</p>
<p>Working on this improved the author's understanding of regular expressions - especially of their eventual "greed". "Greedy" quantifiers are a concept, which has to be explained seperately and is coming unexpected: Whoever is scanning a text for <code>'<.*>'</code>, s/he will search SGML tags, not the whole text. Even with the star's "greediness" the code has to take care, that <code>'.*'</code> doesn't eat the whole text finding no match for <code>'<.*>'</code> at all. Thus the standard syntax with greedy quantifiers cannot be simpler to implement than this with its mere 3 lines 101, 111 and 121 preventing any greed. Perhaps it is faster - otherwise it is difficult to understand, why the concept "greed" is existing at all.</p>
<p>This engine might be useful here and then under circumstances with nothing else available. Its brevity eases translation to other languages and it can work with arbitrary characters for STAR or PERHAPS (for example).</p>
transform a text to another by regex (Python)
2008-09-02T20:30:53-07:00devdoerhttp://code.activestate.com/recipes/users/4166883/http://code.activestate.com/recipes/576481-transform-a-text-to-another-by-regex/
<p style="color: grey">
Python
recipe 576481
by <a href="/recipes/users/4166883/">devdoer</a>
(<a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/regex/">regex</a>).
</p>
<p>transform a text to another by regex</p>
Cheap-date trick; a different way to parse (Python)
2012-03-06T14:08:10-08:00Scott S-Allenhttp://code.activestate.com/recipes/users/4181178/http://code.activestate.com/recipes/578064-cheap-date-trick-a-different-way-to-parse/
<p style="color: grey">
Python
recipe 578064
by <a href="/recipes/users/4181178/">Scott S-Allen</a>
(<a href="/recipes/tags/cheap/">cheap</a>, <a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/sharp/">sharp</a>).
</p>
<p>... a light meal with a heavy dose of "tutorial mash" on the side.</p>
<p>In the constructive spirit of "more ways to solve a problem"; this is a portion of my lateral, occasionally oblique, solutions. Nothing new in le régime de grande, but hopefully the conceptual essence will amuse.</p>
<p>Initially started as a response to <a href="http://code.activestate.com/recipes/577135/">recipe 577135</a> which parses incremental date fragments and preserves micro-seconds where available. That script does more work than this, for sure, but requires special flow-control and iterates a potentially incumbering shopping list (multi-dimensional with some detail).</p>
<p>So here's a different box for others to play with. Upside-down in a sense, it doesn't hunt for anything but a numerical "pulse"; sequences of digits punctuated by other 'stuff' we don't much care about.</p>
<p>Missing a lot of things, intentionally, this snippet provides several examples demoin' flexibility. Easy to button-up, redecorate and extend later for show, till then the delightful commentary makes it hard enough to see bones already -- all six lines or so!</p>
<p><strong>Note:</strong> <em>The core script is repeated for illustrative purposes. The first is step-by-step, the second is lean and condensed for utilitarian purposes. It is the second, shorter, version that I yanked from a file and gussied up.</em></p>
extract table into 2-vector from html page (Python)
2008-09-03T22:22:42-07:00devdoerhttp://code.activestate.com/recipes/users/4166883/http://code.activestate.com/recipes/576485-extract-table-into-2-vector-from-html-page/
<p style="color: grey">
Python
recipe 576485
by <a href="/recipes/users/4166883/">devdoer</a>
(<a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/regex/">regex</a>).
</p>
<p>extract table into 2-vector from html page</p>
Parse profile (Python)
2012-10-12T23:40:55-07:00Jason Friedmanhttp://code.activestate.com/recipes/users/4183835/http://code.activestate.com/recipes/578280-parse-profile/
<p style="color: grey">
Python
recipe 578280
by <a href="/recipes/users/4183835/">Jason Friedman</a>
(<a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/profile/">profile</a>, <a href="/recipes/tags/shell/">shell</a>).
Revision 3.
</p>
<pre class="prettyprint"><code>export VAR1=foo
export VAR2=bar
export VAR3=$VAR1$VAR2
export VAR4=${VAR1}$VAR2
export VAR5=${VAR1}indent
export VAR6="text${VAR1} " # With embedded spaces and a comment
export VAR7='${VAR4}' # Leave text within tics as-is
</code></pre>
<p>will be read as:</p>
<pre class="prettyprint"><code>{'VAR1': 'foo',
'VAR2': 'bar',
'VAR3': 'foobar',
'VAR4': 'foobar',
'VAR5': 'fooindent',
'VAR6': 'textfoo ',
'VAR7': '${VAR4}'}
</code></pre>
Javascript - JSON Parser (only for study) (JavaScript)
2010-07-27T19:36:50-07:00Danillo Souzahttp://code.activestate.com/recipes/users/4174445/http://code.activestate.com/recipes/577337-javascript-json-parser-only-for-study/
<p style="color: grey">
JavaScript
recipe 577337
by <a href="/recipes/users/4174445/">Danillo Souza</a>
(<a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/parse/">parse</a>).
</p>
<p>JSON parser for any object in the script.</p>
<p>OBS: Need a way to identify methods.</p>