Skip to content

Instantly share code, notes, and snippets.

@josemarcosrf
Created December 9, 2020 21:27
Show Gist options
  • Save josemarcosrf/0f93aae188ab0cebe3a96006695b9269 to your computer and use it in GitHub Desktop.
Save josemarcosrf/0f93aae188ab0cebe3a96006695b9269 to your computer and use it in GitHub Desktop.
closure vs recursive functions to process a tree-like recursive structure
import json
from collections import deque
from functools import wraps
from pprint import pformat
from time import time
from typing import List
from typing import Text
def timeit(f):
@wraps(f)
def wrap(*args, **kw):
ts = time()
result = f(*args, **kw)
td = time() - ts
print(f'func: {f.__name__} - took: {td:.4f} sec')
return result
return wrap
@timeit
def get_recursively(section):
# define the extractor function
def getter(section, processed):
for sec in section.get("sections"):
processed.append(sec.get("name"))
getter(sec, processed)
# process
processed = []
getter(section, processed)
return processed
@timeit
def get_closurely():
# define the extractor function
def getter(to_process):
processed = []
def collector(this_section) -> List[Text]:
nonlocal processed
nonlocal to_process
processed.append(this_section.get("name"))
for sub in this_section.get("sections", [])[::-1]:
to_process.append(sub)
return processed
return collector
to_process = deque(data.get("sections")[::-1])
get_secs = getter(to_process)
# process
all_sections = []
while len(to_process):
sec = to_process.pop()
all_sections = get_secs(sec)
return all_sections
if __name__ == "__main__":
with open("recursive_doc_example.json", "r") as f:
data = json.load(f)
close_sections = get_closurely()
print(pformat(close_sections))
print("---------------")
recursive_sections = get_recursively(data)
print(pformat(recursive_sections))
{
"name": "IN PRACTICE - contracts WITHDRAWAL 08 2020",
"sections": [
{
"name": "New Savings Plan Retreat PER",
"text": "",
"sections": [
{
"name": "1/ What is the new retirement savings plan?",
"text": "\nWhat is the PER?\n* The PACTE (Action Plan for Enterprise Growth and Transformation) law has created the RIP (Retirement Savings Plan) proposed to savers since 1/10/2019.\n* The objective of this Pension Savings Plan is to standardize all retirement savings schemes and allow the transfer of your savings throughout your working life to keep only one retirement savings contract.\n* So far, there were two individual products: the Popular Retirement Savings Plan (PERP) and the Madelin Contract for Non-Employed Workers. And corporate contracts: Articles 83, and the PERCO collective retirement savings plan.\n* The Covenant Act created a single product. You will be able to put in the individual payments, and the company's payments, which may be mandatory payments, or voluntary payments on a voluntary basis.\n* The new RIP consists of three products:\n* The individual RIP which is opened \"voluntaryly\" by the saver, during his working life, whether he is employed or self-employed. He succeeds the current PERP and Madelin contracts.\n* The universal collective enterprise RIP for all employees, which succeeds PERCO\n* The compulsory RIP, a category which succeeds the contracts \"Article 83\" for an objective category of employees.\nHow can I save on the PER?\n* The retirement savings plan can be fed in three different ways with:\n* Voluntary payments\nAmounts from the participation the employer's interest and abundance as well as the rights entered in the time savings account (or amounts corresponding to days of rest not taken in the absence of a TEC),\n* Compulsory payments (from the employee or employer) for compulsory corporate retirement savings plans.\n* The amounts placed in each RIP are divided into three compartments:\n* The \"Individual\" compartment collects voluntary payments (the savings placed on the current PERP, Madelin, and voluntary voluntary payments by employees on the current \"Article 83\").\n* The \"Collective\" compartment collects wage savings: participation/interest/abundance /Time savings account (or amounts corresponding to days of rest not taken in the absence of TEC), (current PERCO)\n* The \"Category\" or \"Required\" compartment collects the mandatory contributions of the employee and the employer (current \"Article 83\")\n\nCan I place the payments of my RIP (Retirement Savings Plan) on the support of my choice? How will my savings be managed?\n* By default, the \"balance\" profile of retirement management (CAPTE management) applies to the RIP. This management method allows you to secure your savings according to the length of time that separates you from the retirement age:\n* At the beginning of the savings phase, when retirement is far away, savings will be oriented towards assets with better expectations of return, such as corporate shares.\nAs retirement age approaches, savings will be progressively secured.\n* There are other management profiles based on your preferences (precautionary profile, dynamic profile, or free management if you want to manage the choice of funds yourself).\nIs retirement savings blocked in the RIP? Is it possible to unlock it before retirement?\n* Amounts paid on a retirement savings plan are in principle blocked until retirement age.\n* However, savings can be released at any time before retirement, in case of:\n* Death of the spouse of the holder of the RIP or his/her ACAP partner.\n* Invalidity of the holder, one of his children, his spouse or his partner of PACS (This invalidity is assessed within the meaning of the 2° and 3° of Article L. 341-4 of the Social Security Code).\n* Overindebtedness situation of the holder within the meaning of Article L.711-1 of the Consumer Code.\n* Expiry of entitlement to unemployment insurance, or absence of activity for 2 years from the end of the duties of director, member of the Executive Board or of the Supervisory Board.\n* Discontinuation of the holder's self-employed activity following a judicial liquidation or with the agreement of the president of the commercial court in conciliation proceedings.\n* Acquisition of principal residence (except for compartment 3 under PERE - Mandatory payments).\nWhat are the exit modalities at the end of the RIP?\n* You have the freedom of choice, between payment in the form of capital (once or in a split manner) and/or withdrawal into a lifetime annuity.\n* Please note, if you have opted for subscription for an entire life annuity in an individual RIP, the outing will have to be an annuity.\n* The sums invested on the \"Required Companion\" allow only an exit into a lifetime rent.",
"sections": [
{
"name": "L3",
"sections": []
}
]
},
{
"name": "2 /What can the PER change to my personal situation?",
"text": "\nI already save in a pension contract (PERP or Madelin): can I keep or change my contract?\n* You can choose to keep and continue your current contract, the rules of which will be unchanged.\n* You can also transfer your savings into a new pension savings plan to benefit from the new rules opened by the PACTE law.\nWhat are the benefits of the transfer?\nYou can unblock your savings at any time for the purchase of your principal residence (except for compartment 3 - Mandatory payments).\nAt the time of retirement, you can choose between an annuity or a capital outlay.\nIs there a fee?\nThe transfer fees applied are those of your original contract.\nTransfer fees limited to 1% of the outstanding amount. No one 5 years after the first payment, or after the date of\npension liquidation in a compulsory old-age insurance scheme (or the age of departure at the time of retirement).\n(retirement)....................................................................................................................................................................................................................................................\nIf the transfer takes place between two contracts with Generali, there is no charge.\nMy company has already set up a PERCO for all staff and a \"Article 83\" contract for managers, what can the PER change?\nToday: some employees of the company combine two retirement savings plans, one PERCO and one article 83 with different rules. In the event of departure from the company, they cannot pool their pension savings.\n* With a PER: the company can combine the two products into a single retirement savings plan, to simplify its monitoring and that of its employees. This also allows employees to transfer or pool their savings when they leave the company.\n\n",
"sections": []
}
]
},
{
"name": "Life of the pension contract",
"text": "",
"sections": [
{
"name": "1/My payments",
"text": "\nI want to change my remittance terms, what should I do?\n* You can send your request via our website generali.fr, section \"my online requests\", located at the bottom of our homepage logo \"individual retirement\" and then choose:\n* Or contact your usual insurance partner who will advise you.\nCan I make voluntary payments?\n* Payments may be free (voluntary) or periodic depending on the contract. By default, payments are deductible unless you opt for non-deductibility.\n* Contact your usual insurance partner who will advise you.",
"sections": []
},
{
"name": "2/I want to modify my contract",
"text": "\nI want to change my mailing address, what should I do?\nYou can send your request via our website generali.fr, section \"my online requests\", located at the bottom of our homepage logo \"individual retirement\" or \"collective retirement\" and then choose:\nOr contact your usual insurance partner who will advise you.\nI want to change my bank details what should I do?\n* You can send your request via our website generali.fr, section \"my online requests\", located at the bottom of our homepage logo \"individual retirement\" and then choose:\n* Or contact your usual insurance partner who will advise you.\nI want to change the beneficiary clause, what should I do?\n* You can send your request via our website generali.fr, section \"my online requests\", located at the bottom of our homepage logo \"retirement\" and then choose: (for a collective retreat).\n* Or contact your usual insurance partner who will advise you.\n\n",
"sections": []
},
{
"name": "3/ Can I recover my savings before retirement?",
"text": "\nUnder what conditions can I early unlock my pension savings\nOn a Madelin or PERP contract\nSavings may be released before retirement in the following special cases, as determined by Article L132-23 of the Insurance Code:\nExpiry of the insured person's rights to unemployment benefits granted as a result of involuntary loss of employment or the fact that an insured person has been employed as a director, director or supervisory board member and has not liquidated his pension under a compulsory old-age insurance scheme, has not held an employment contract or a social mandate for at least two years, starting from the failure to renew his social mandate or the revocation;\nDiscontinuation of the insured person's self-employed activity following a judgment of judicial liquidation pursuant to the provisions of Book VI of the Commercial Code or any situation justifying such redemption according to the President of the Commercial Court at which a conciliation procedure is instituted as referred to in the application, which makes the request with the agreement of the insured person;\nInvalidity of the insured person corresponding to the classification in the second or third categories provided for in the contract;\nDeath of the spouse or partner bound by a civil solidarity pact;\nOverindebtedness situation of the holder defined at the request of the insurer either by the President of the Individual Overindebtedness Commission or by the judge where the release of individual rights resulting from such contracts appears necessary for the discharge of the liability of the person concerned;\nFor small PORs, a 6th case of early release is possible, if the following conditions are met :\n* The value of the amounts entered on the PERP must be less than € 2000\n* For PRPPs that do not provide for regular payments, no payments must have been made during the four years prior to the redemption.\n* For PERPs providing for regular payments, membership of the contract must have taken place at least four years before the request for redemption.\n* The income of the tax home of the year preceding the year of redemption is subject to a ceiling. It must be less than the sum, increased where appropriate in respect of the additional half-shares deducted for the calculation of the income tax relating to that income, provided for in Article 1417 II of the General Tax Code.\n\n\nOn a PER\nIt is possible to unlock savings before retirement, in the following special cases, as determined by Article L224-4 of the Monetary and Financial Code ( these are the same cases as the Madelin contracts, with 2 novelties related to the PER, indicated in bold):\n* The death of the spouse of the holder or his partner bound by a civil solidarity pact\n* Disability of the holder, but also of his children, spouse or partner bound by a civil pact of solidarity. This invalidity is assessed within the meaning of Article L. 341-4 of the Social Security Code, 2° and 3°;\n* The over-indebtedness situation of the holder within the meaning of Article L. 711-1 of the Consumer Code;\n* The expiry of the holder's unemployment insurance rights, or the fact that the holder of a plan has been a director, member of the Executive Board or member of the Supervisory Board and has not liquidated his pension under a compulsory old-age insurance scheme, that he has not held an employment contract or a social mandate for at least two years from the date of his non-renewal or revocation of his social mandate\n* The termination of the self-employed activity of the holder following a judgment of judicial liquidation pursuant to Title IV of Book VI of the Commercial Code or any situation justifying such withdrawal or redemption according to the President of the Commercial Court at which a conciliation procedure referred to in Article L. 611-4 of the Commercial Code is instituted, which makes the request with the consent of the holder;\n* Acquisition of the principal residence (except for compartment 3- Compulsory payments).\nWhat documents do I need to provide to unlock my pension savings early?\nYou can make your request on generali.fr my online requests logos \"retirement\"; choose: for a collective retreat. The documents to be provided are indicated.\n* Or contact your usual insurance partner who will advise you.",
"sections": []
},
{
"name": "4 / My attestations",
"text": "\nI want to get a Madelin or tax certificate what do I have to do?\n* If you have entered into a Madelin retirement contract, you can send your request via our website generali.fr, section \"my online requests\", located at the bottom of the logo homepage \"individual retirement\" and then choose:.\n* You can also contact your usual insurance partner.\nI want to get an IFU certificate what should I do?\nYou can contact your usual insurance partner.",
"sections": []
},
{
"name": "5/ I would like to know the saving value of my contract",
"text": "\nI want to know the saving value of my contract, what should I do?\n* You receive a status statement of your contract every year.\n* You can also contact your usual insurance partner.",
"sections": []
}
]
},
{
"name": "Taxation of the pension contract",
"text": "",
"sections": [
{
"name": "1 / What is the taxation of my Madelin contract?",
"text": "\nWhat is the tax at entry? Can I get a tax deduction?\n* You are self-employed, you have the possibility to deduct your contributions from your taxable benefit (within the limits laid down in Article 154a or 154a OA of the General Tax Code).\n* The tax deduction limit is as follows:\n* 10% of occupational income, limited to 8 PASS (annual social security ceiling), increased by 15% of income between 1 and 8 PASS.\n* or 10% of PASS (annual social security ceiling)\n* The Madelin contract can be extended to your spouse if he or she is a partner. The contributions paid by your partner to the Madelin pension contract are deductible from taxable profit, in the same tax envelope as yours.\nWhat is the tax on Madelin retirement pensions?\n* The benefits of a Madelin pension contract are paid in the form of lifetime pensions.\n* They are liable to income tax, as well as other retirement benefits, after a reduction of 10% and are subject to social security contributions\n2 / What is the taxation of my PERP contract?\nWhat is the tax at entry? Can I get a tax deduction?\n* Disbursements are deductible from taxable income, up to:\n* 10% of the previous year's occupational income, limited to 8 PASS (annual social security ceiling),\n* or 10% of PASS (annual social security ceiling)\nWhere the limit is not reached, the unused deduction amount may be carried forward for three years and added to the allowable deductions.\n* The deduction limit is global for all optional individual pension contributions: it also includes any contributions made under the Madelin or Art. 83 contracts, as well as employer abundances on a PERCO.\n\nWhat is the tax on exit?\n* On exit, the life annuity is subject to income tax in the category of life annuities, pensions and pensions. It benefits from the 10% reduction. And it is subject to social levies.\n* A partial capital outlay of 20% is possible. The amounts are then subject to income tax, or optionally, to a 7.5% exemption from capital (after a 10% reduction).\n\n\n3 / What is the taxation of my PER contract?\nWhat is the tax on entry?\nThe amounts you voluntarily pay on a PIP, group or individual, allow you to deduct payments from your taxable income (deduction from total net income or category income), such as the current tax deduction for Perp or Madelin under certain limits.\nYou can also opt out of deducting these payments on entry, in order to benefit from a lower tax on exit. In the event of an option for the non-deductibility of payments of your taxable income, it must be made for each payment.\nThe amounts from the employer (interest, participation, compulsory payment) do not qualify for this deductibility, but they remain exempt from income tax.\nWhat is the tax on exit?\nThe applicable tax is different depending on the origin of the payments made to the RIP.\nIf you choose retirement pension:\nfor payments that have been deducted from your taxable income at entry, annuities may be taxed on income under the common system of retirement pensions + social contributions from investment income at the aggregate rate of 17.2% on the taxable portion of the pension determined according to the age of the beneficiary (taxable fraction of 70% before 50 years, 50% between 50 and 59 years, 40% between 60 and 69 years and 30% after 69 years).\nfor payments not deducted at entry, annuities are taxable on income tax, after a reduction that depends on your age on the first payment of the annuity (taxable fraction equal to 70% before age 50, 50% between age 50 and 59, 40% between age 60 and 69 and 30% after age 69).\nIf you choose retirement: voluntary payments, which have been deducted from your income (deduction of total net income or category 1 income), will be subject to income tax (in the category of retirement pensions without the 10% reduction) and the capital gains generated by these payments will be subject to the single flat-rate levy of 12.8% (or optionally on the income tax scale) + social levies at the overall rate of 17.2%, i.e. an overall tax of 30%.\nIn the event of an early exit of the RIP:\nFor acquisition of the principal residence:\nthe share of capital corresponding to the amounts paid in the Plan and deducted from taxable and redeemed income is subject to the Income Tax and the capital gains related to these payments are subject to the single flat-rate levy of 12.8% (or optionally on the income tax scale) + social levies at the overall rate of 17.2%, i.e. an overall tax of 30%;\nthe share of capital corresponding to the amounts paid in the Plan and not deducted from taxable and redeemed income is exempt from income tax and the capital gains related to these payments are subject to the single flat-rate levy of 12.8% (or optionally on the income tax scale) + social levies at the overall rate of 17.2%, i.e. an overall tax of 30%.\nFor wage savings, there will be no taxation but capital gains are subject to social levies at the overall rate of 17.2%.\nFor other reasons of release (spouse's death, etc.) ), there is no taxation (income tax exemption). Capital gains are subject to social levies at an overall rate of 17.2%.\nAmounts derived from payments made by the employer (for example, on a \"group enterprise RIP\" through interest or participation) are exempt from income tax. However, capital gains will be subject to social levies at an overall rate of 17.2%.",
"sections": []
}
]
}
],
"translated": true,
"language": "en"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment