pyprika package

The pyprika package contains the primary API.

Input and output

pyprika.load(fp, loader=None, **kw)

Load fp, a file-like object

The file is assumed to be a pyprika-compliant YAML document. If the document contains a sequence, a list of Recipe objects will be returned. Otherwise, a single Recipe object should be returned.

Note that this function wraps the underlying exceptions thrown by Recipe.from_dict() under the assumption it is due to a malformed document, but the original traceback is preserved.

Parameters:
  • fp (file-like) – the file-like object containing the document to load
  • loader (callable) – takes one positional argument and optional arguments and returns a dict (defaults to yaml.load)
  • **kw

    passed through to loader

Raises LoadError:
 

if there was an error in the loading of the document, usually indicative of a syntax error

Returns:

the recipe data contained in the stream

Return type:

Recipe or list of Recipe

pyprika.loads(data, loader=None, **kw)

Load recipe from string data.

This wraps data in a cString.StringIO and calls load() on it.

See load() for more information.

Parameters:data (str) – recipe document data
Returns:the recipe data contained in data
Return type:Recipe or list of Recipe
pyprika.dump(recipe, fp, dumper=None, **kw)

Dump recipe to a file-like object

Parameters:
  • recipe (Recipe) – the recipe to dump
  • fp (file-like) – the file stream to dump to
  • dumper (callable) – a callable which takes two positional arguments, the first a dict and the second a file stream, and optional keyword arguments and encodes the recipe to the file stream (defaults to yaml.dump)
  • **kw

    passed through to dumper

pyprika.dumps(recipe, dumper=None, **kw)

Dump recipe object as a string.

This is a convenience method to dump to a StringIO object.

See dump() for parameter details.

Returns:recipe encoded as a string
Return type:str

API classes

Recipe

class pyprika.Recipe(name)

Class for representing a recipe.

Variables:
  • name (str) – human-friendly name of recipe
  • index – optional application-specific indexing value
  • servings – number of servings or a range (a 2-item tuple)
  • source (str) – human-friendly source of recipe
  • source_url (str) – URL source to for recipe
  • prep_time (Quantity) – total preparation time for recipe
  • cook_time (Quantity) – total cooking time for recipe
  • notes (str) – miscellaneous data about recipe
  • ingredients (list) – list of Ingredient objects
  • directions (list) – list of instructions to prepare recipe
classmethod from_dict(d)

Creates a new recipe from a dictionary.

Parameters:

d (dict) – the dictionary to convert

Raises:
  • FieldError – if a field is missing, invalid, or not well-formed
  • ParseError – if a Pyprika syntax error is present
Returns:

the resulting recipe

Return type:

Recipe

to_dict(serialize=False)

Return a dictionary representing the Recipe.

Parameters:serialize (bool) – convert as much as possible to primitive types
Returns:a dictionary mapping attribute names to values
Return type:dict

Ingredient

class pyprika.Ingredient(label, quantity=None)

Class for representing ingredients.

Variables:
  • label (str) – the label of the ingredient (like egg)
  • quantity (Quantity) – the amount of the ingredient, if any
classmethod parse(s)

Parse an object from a string. Valid strings are of the form:

[(quantity) ]label

Where quantity must be valid syntax to Quantity.parse() and label is any text not beginning with a value enclosed in parenthesis.

Parameters:s (str) – string to parse
Raises ParseError:
 on invalid syntax
Returns:the resulting Ingredient
Return type:Ingredient

Quantity

class pyprika.Quantity(amount=None, unit=None)

Class for representing quantity.

Quantities can either be a measurement (like 1 cup) or a dimensionless amount (like 12).

Variables:
  • amount – numeric amount of the quantity
  • unit (str) – unit of the amount, if any
classmethod parse(s)

Parse an object from a string. Valid strings are of the form:

amount[ unit] 

Where unit is unconstrained and amount is one of the following:

  • an integer, like 4
  • a decimal number, like 4.5 (not scientific notation)
  • a fraction, like 1/2
  • a mixed number, like 1 1/2
Parameters:s (str) – string to parse
Raises ParseError:
 on invalid syntax
Returns:the resulting Quantity
Return type:Quantity

Exceptions

FieldError

class pyprika.FieldError

Raised when a constraint on a field is not met.

ParseError

class pyprika.ParseError

Raised on invalid syntax.

LoadError

class pyprika.LoadError(*args, **kwargs)

A blanket exception to catch if there was an error loading a recipe.

Variables:cause – the original exception, if any