小编Bak*_*riu的帖子

我如何在Python3.3中使用fork()

我如何在Python3.3中使用fork()这是我的代码:

输入:

#!/usr/bin/env python
import os

def Child_process():
    print("We are in Child_Process")
    print("My PID: %d"%os.getpid())
    print("Child_Process is exiting")

def Parent_process():
    print("-------Parent_process---------")
    wpid = os.fork()
    if wpid==0:
        print("wpid is 0 means We are in Child_process")
        print("Child :%d"%wpid)
        Child_process()
    else:
        print("Execute Parent_process")
        print("Parent_process %d"%wpid)
        Parent_process()

Parent_process()
Run Code Online (Sandbox Code Playgroud)

输出:

C:\Python33\python.exe C:/Users/Iem-Prog/Desktop/Py/Fork

Traceback (most recent call last):

  File "C:/Users/Iem-Prog/Desktop/Py/Fork", line 21, in <module>
-------Parent_process---------
    Parent_process()
  File "C:/Users/Iem-Prog/Desktop/Py/Fork", line 11, in Parent_process
    wpid = os.fork()

AttributeError: 'module' object has no attribute 'fork'
Run Code Online (Sandbox Code Playgroud)

python windows

1
推荐指数
1
解决办法
4240
查看次数

如何处理IndentationError:unindent与任何外部缩进级别都不匹配

我到处搜索,我无法在任何地方找到答案,我的代码遇到了问题,我正在尝试用peewee做一个简单的清单.我所拥有的就是这个错误.

  File "inventario.py", line 38
if selection =='1':
                  ^
IndentationError: unindent does not match any outer indentation level
Run Code Online (Sandbox Code Playgroud)

我已经读过与空格混合的标签可以实现这一点,已经检查过,甚至使用SublimeText工具必须用空格替换制表符.

from peewee import *

db = SqliteDatabase('stockpy.db') #creates db

class Product(Model): #data to create product table
    id_prod = IntegerField()
    name = CharField()
    price = IntegerField()
    stock = IntegerField()

    class Meta:
        database = db #tells which db to use 

Product.create_table() #creates table

def newProd(id_prod, name, price, stock):
     Product.create(id_prod =  id_prod, name = name, price = price, stock = stock) #adds new product …
Run Code Online (Sandbox Code Playgroud)

python database peewee

1
推荐指数
1
解决办法
4975
查看次数

Haskell Ord实例悖论在Eq

我希望能够订购Polynomes,首先按长度(度)进行比较,然后按系数进行比较.Polynomes是双打的列表[1,2,3] = 3x²+2x+1.但是如果最后一个元素为零,那么它应该被删除,所以我写了一个函数来做那个调用realPolynom.realPolynom [1,2,3,0] = [1,2,3] 现在,我的Ord实例看起来像:

instance Ord Polynom where                                  
    compare a b = compare ((realLength a), reverse (pol2list (realPolynom a))) ((realLength b), reverse (pol2list (realPolynom b)))
Run Code Online (Sandbox Code Playgroud)

realLength 只是最后没有零的多项式的长度.

pLength :: Polynom -> Int       
pLength (Polynom(a)) = length a

realLength :: Polynom -> Int                        
realLength a = pLength(realPolynom(a))
Run Code Online (Sandbox Code Playgroud)

pol2listPolynom p = p

pol2list :: Polynom -> [Double]
pol2list (Polynom p) = p 
Run Code Online (Sandbox Code Playgroud)

问题是:

  • [0,2,0] < [0,2,3] 是的,这很好

  • [0,2,0] < [0,2] 虚假,也很好

  • [0,2,0] …

haskell equality polynomials

1
推荐指数
1
解决办法
197
查看次数

使用foldr获取符合条件的第一个项目

我正在进行自我练习,并想知道是否有办法在列表中找到符合某个标准的左边第一个项目foldr?我希望在找到第一个项目时停止递归(我知道我可以结合使用take),但我很想知道是否可以使用它foldr

firstFind (\x -> x > 1000) [] xs 
Run Code Online (Sandbox Code Playgroud)

haskell fold

1
推荐指数
1
解决办法
443
查看次数

使用列表检查字符串是否包含某个字母或数字

我正在回答的问题要求您验证 Car Reg Plate。它必须查找由两个字母、三个数字、三个字母组成的序列,否则返回“不是有效的邮政编码”之类的消息

我需要知道如何通过将字符串与列表进行比较来检查字符串是否包含某个字母或数字。

到目前为止,我有这个:

# Task 2

import random
import re

def regNumber():
    # Generate a Car Reg Number
    letters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
    numbers = ["1","2","3","4","5","6","7","8","9","0"]

    letter1 = random.choice(letters)
    letter2 = random.choice(letters)
    number1 = random.choice(numbers)
    number2 = random.choice(numbers)
    letter3 = random.choice(letters)
    letter4 = random.choice(letters)
    letter5 = random.choice(letters)
    licensePlate = (letter1 + letter2 + number1 + number2 + letter3 + letter4 + letter5)
    return licensePlate, letters, numbers

carReg, letters, numbers = regNumber()

print(carReg)
if letters not in carReg: print("Success")
Run Code Online (Sandbox Code Playgroud)

但是,我收到此错误: …

python

1
推荐指数
1
解决办法
3425
查看次数

从定义中编写函数?

首先,我想让大家都知道我对Haskell很新,所以为了增加知识等,我一直在尝试问题,而且我很困惑.我想我差不多了,但一些更有经验的建议将不胜感激.这是问题:

一个体育团队的名字和他们在上一场比赛中得分的数量就像这样("Newcastle",[3,3,3,0]).此数据由类型定义建模:

type TName = String
type Points = [Int]
type Team = (TName,Points)
Run Code Online (Sandbox Code Playgroud)

从这里我必须定义以下函数,如果他们的积分总和更大,则命令一个团队高于另一个团队:

sortPoints :: [Team] -> [Team]
Run Code Online (Sandbox Code Playgroud)

这就是我尝试过的:

sortPoints :: [Team] -> [Team]
sortPoints [_,()] -> []
sortPoints [_,(x:xs)] = sum[x|x<-xs]
Run Code Online (Sandbox Code Playgroud)

一旦我到达这里,我不太确定如何添加检查点总和的条件,任何指针都会非常感激,因为我仍然会接受很多Haskell功能.

haskell types function definition

1
推荐指数
1
解决办法
113
查看次数

如何计算单词中的字母?

我正在尝试制作一个Python脚本,用于计算我的Hangman游戏随机选择的单词中的字母数量.

我已经在网上浏览了一下,但我发现的大部分内容都是用一个字来计算特定的字母.经过更多的环顾四周后,我最终得到了这个,但由于某些原因这不起作用.如果有人能够指出错误,那将非常感激.

wordList = ["Tree", "Fish", "Monkey"]
wordChosen = random.choice(wordList)
wordCounter = wordChosen.lower().count['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
print(wordCounter)
Run Code Online (Sandbox Code Playgroud)

python counter words letters

1
推荐指数
1
解决办法
3357
查看次数

如果分母为零,或者分子和分母都为零,如何划分算子场景?

我正在尝试使用HSpec在haskell中进行TDD.因此,在为除法运算符编写特殊情况时,例如:

  • 3 / 0 => Infinity
  • 0 / 0 => Nan

如何使用Hspec测试上述案例?

haskell hspec

1
推荐指数
1
解决办法
73
查看次数

代表类型和出现:(如此)易于理解,(因此)难以编码

通过示例简要介绍类型和事件.

EX1. abbacb

a,b,c是类型.

a发生2次; b发生3次; c发生1次.

这可以更简洁地表示为[('a',2),('b',3),('c',1)](实际上,顺序无关紧要).

EX2. abbacb

ab,bb,ba,ac,cb是的类型的序列

每个序列只出现一次.

这可以表示为 [("ab",1),("bb",1),("ba",1),("ac",1),("cb",1)]

以下图形结构具有前两个相同的信息内容:

('a',2)          -- 'a'  occurs 2 times
     ('b',1)     -- "ab" occurs 1 times
     ('c',1)     -- "ac" occurs 1 times
('b',2)          -- 'b'  occurs 2 times
     ('a',1)     -- "ba" occurs 1 times
     ('b',1)     -- "bb" occurs 1 times
('c',1)          -- 'c'  occurs 1 times
     ('b',1)     -- "cb" occurs …
Run Code Online (Sandbox Code Playgroud)

haskell types linguistics

1
推荐指数
1
解决办法
104
查看次数

是否有可能从鼠兔的每个队列消费?

我正在尝试设置一个程序,它将从RabbitMQ中的每个队列中消耗,并根据某些消息运行某些脚本.不幸的是,当添加消费者时,如果它遇到单个错误(即没有找到超时或队列),则整个通道都已死亡.此外,队列来来去去,所以它必须经常刷新队列列表.这甚至可能吗?到目前为止,这是我的代码.

import pika
import requests
import sys

try:
    host = sys.argv[1]
except:
    host = "localhost"

def get_queues(host="localhost", port=15672, user="guest", passwd="guest", virtual_host=None):
    url = 'http://%s:%s/api/queues/%s' % (host, port, virtual_host or '')
    response = requests.get(url, auth=(user, passwd))
    return response.json()

queues = get_queues(host)

def get_on_message(queue):
    def on_message(channel, method_frame, header_frame, body):
        print("message from", queue)
        channel.basic_ack(delivery_tag=method_frame.delivery_tag)
    return on_message

connection = pika.BlockingConnection(pika.ConnectionParameters(host))
channel = connection.channel()

for queue in queues:
    print(channel.is_open)
    try:
        channel.basic_consume(get_on_message(queue["name"]), queue["name"])
        print("queue added",queue["name"])
    except Exception as e:
        print("queue failed",queue["name"])
        sys.exit()

try:
    channel.start_consuming()
except KeyboardInterrupt: …
Run Code Online (Sandbox Code Playgroud)

python rabbitmq pika

1
推荐指数
1
解决办法
414
查看次数