This is the 24th day of my participation in Gwen Challenge
Requirements describe
If you need to implement a random-generated recipe for three meals, offer both random-generated or custom random-generated options, as far as possible from Pythonic. For example, we can call it like this:
breakfast >> lunch >> dinner
Copy the code
Demand analysis
The syntax implementation of >> requires the use of __rshift and __rrshift. For specific usage, please refer to the previous two tweets. Rrshift Python implements Linux pipes
- Customize a list of meals as a random selection
- If it is all randomly generated, no parameter is passed and steps 4 and 5 are carried out
- The result of breakfast is passed to lunch, and lunch is inserted based on the result of breakfast
- Pass the result of lunch to Dinner, insert dinner at the base of the result of lunch
- If it is custom and random, a function is required to record the parameters and repeat step 45
Implement >> pipe syntax
For syntax examples, see rrshift Python’s Magic method for implementing Linux pipes in Python
# -*- coding=utf-8 -*-
import functools
import random
import pprint
class Pipe:
def __init__(self, function) :
self.function = function
functools.update_wrapper(self, function)
# when one of the >> sides does not implement __rshift__
def __rrshift__(self, other) :
return self.function(other)
# implementation carries custom parameters
def curry(self, *args, **kwargs) :
return Pipe(lambda x: self.function(x, *args, **kwargs))
Copy the code
Menu waiting for objects
breakfast_menu = ['bacon'.'egg'.'milk'.'porridge'.'bread'.'corn']
lunch_menu = ['fish'.'beef'.'chicken'.'radish'.'spinach'.'watercress']
dinner_menu = ['lean meat'.'mutton'.'celery'.'tofu'.'cucumber']
Copy the code
Menu random implementation
@Pipe
def breakfast(menu, *args, **kwargs) :
menu['breakfast'] = random.sample(breakfast_menu, 3 if args == (' '.)else 3 - len(args))
if not args == (' ',) :for custom in args:
menu['breakfast'].append(custom)
return menu
@Pipe
def lunch(menu, *args, **kwargs) :
menu['lunch'] = random.sample(lunch_menu, 3 if args == (' '.)else 3 - len(args))
if not args == (' ',) :for custom in args:
menu['lunch'].append(custom)
return menu
@Pipe
def dinner(menu, *args, **kwargs) :
menu['dinner'] = random.sample(dinner_menu, 3 if args == (' '.)else 3 - len(args))
if not args == (' ',) :for custom in args:
menu['dinner'].append(custom)
return menu
Copy the code
Overall control and pipeline implementation
if __name__ == "__main__":
# menu stores the menu
menu = dict()
random_choice = int(input("Is the menu automatically generated? Yes: 1 No: 0\n"))
# Automatic random generation
if random_choice:
pprint.print(menu >> breakfast >> lunch >> dinner)
# Custom menu, pass parameters
else:
breakfast_custom_list = input("Breakfast menu (3 items at most), not 3 items left, random generation:").split(' ')
lunch_custom_list = input("Lunch menu (3 items at most), not 3 items left, random generation:").split(' ')
dinner_custom_list = input("Dinner menu (3 items at most), not 3 items left, random generation:").split(' ')
[1,2,3] => 1,2,3
breakfast = breakfast.curry(*(breakfast_custom_list))
lunch = lunch.curry(*(lunch_custom_list))
dinner = dinner.curry(*(dinner_custom_list))
The output of the beautiful format is not done, directly print
pprint.print(menu >> breakfast >> lunch >> dinner)
Copy the code
Randomly generated effect
Are menus automatically generated? Is: 1 no: 0 1 {' breakfast ': [' bread', 'egg', 'how'], 'dinner' : [' now ', 'cucumber', 'lean meat'], 'lunch' : ['spinach', 'chicken', 'fish']}Copy the code
Custom menu generation effect
Are menus automatically generated? Yes: 1 No: 0 0 Breakfast menu (maximum 3 items), not enough 3 items, left random generation: sweet potato corn porridge lunch menu (maximum 3 items), not enough 3 items, left random generation: dinner menu (maximum 3 items), not enough 3 items, left random generation: loofah soup {'breakfast': [' sweet potato ', 'corn', 'white porridge'], 'dinner' : [' now ', 'cucumber', 'towel gourd soup'], 'lunch' : [' beef ', 'fish', 'radish']}Copy the code
Implementation of carrying parameters
# implementation carries custom parameters
def curry(self, *args, **kwargs) :
return Pipe(lambda x: self.function(x, *args, **kwargs))
Copy the code
How it works: The *args and **kwargs of anonymous functions hold the parameters that need to be carried.
Other syntax, not mentioned, welcome message communication