jes

How to mock/spy useState hook in jest?

亡梦爱人 提交于 2021-01-27 18:41:12
问题 I am trying to spy on useState React hook but i always get the test failed This is my React component: const Counter= () => { const[counter, setCounter] = useState(0); const handleClick=() => { setCounter(counter + 1); } return ( <div> <h2>{counter}</h2> <button onClick={handleClick} id="button">increment</button> </div> ) } counter.test.js : it('increment counter correctlry', () => { let wrapper = shallow(<Counter/>); const setState = jest.fn(); const useStateSpy = jest.spyOn(React,

Python global variable/scope confusion [duplicate]

南楼画角 提交于 2020-04-11 07:37:11
问题 This question already has answers here : Python scope: “UnboundLocalError: local variable 'c' referenced before assignment” [duplicate] (4 answers) Closed 4 years ago . I've started teaching myself python, and have noticed something strange to do with global variables and scope. When I run this: x = 2 y = 3 z=17 def add_nums(): y = 6 return z+y The result of 23 is printed... However, when I expand the return to be: x = 2 y = 3 z=17 def add_nums(): y = 6 z = z + y return z I get the following

Flip horizontally an image in Python (JES)

*爱你&永不变心* 提交于 2019-12-31 01:28:30
问题 I need to make a function that will copy an image, but mirrored. I created the code to mirror the image, but it isn't working and I don't know why because I traced the code and it should be mirroring the image. Here's the code: def invert(picture): width = getWidth(picture) height = getHeight(picture) for y in range(0, height): for x in range(0, width): sourcePixel = getPixel(picture, x, y) targetPixel = getPixel(picture, width - x - 1, height - y - 1) color = getColor(sourcePixel) setColor

Scaling Part of a Picture

倖福魔咒の 提交于 2019-12-30 07:41:15
问题 I want to scale up a part of a picture, in this example, a nose. I have a function to select the part of the picture I want to enlarge. def copyAndPaste(picture): height = getHeight(picture) width = getWidth(picture) newPicture = makeEmptyPicture(width, height) for x in range(width): for y in range(height): pxl = getPixel(picture,x,y) if (x>48 and x<59) and (y>58 and y<71): newPxl =getPixel(newPicture, #?,#?) else: newPxl = getPixel(newPicture, x,y) color = getColor(pxl) setColor(newPxl,color

Mirroring an Image by its Diagonal in Jython

若如初见. 提交于 2019-12-22 11:27:56
问题 So I need to mirror an image. The top right side of the image should be flipped over to the bottom left side. I created a function that flips the top left side of an image to the bottom right, but I just can't seem to figure out how to do it the other way. Here's the code: def mirrorPicture(picture): height = getHeight(canvas) width = height # to make mirroring easier, let us make it a square with odd number # of rows and columns if (height % 2 == 0): height = width = height -1 # let us make

White gridlines over image using JES (python)

淺唱寂寞╮ 提交于 2019-12-13 17:28:29
问题 How can I write a program using JES to draw “White” gridlines on an image where the horizontal gridlines are separated by 10 pixels and the vertical gridlines are separated by 20 pixels? 回答1: Yes, surprisingly, addLine(picture, startX, startY, endX, endY) can only draw black lines !? So let's do it by hand. Here is a very basic implementation: def drawGrid(picture, color): w = getWidth(picture) h = getHeight(picture) printNow(str(w) + " x " + str(h)) w_offset = 20 # Vertical lines offset h

Jython (JES) - 90 degree rotation function

感情迁移 提交于 2019-12-11 11:52:05
问题 I need to write a function spin(pic,x) where it will take a picture and rotate it 90 degrees counter clockwise X amount of times. I have just the 90 degree clockwise rotation in a function: def rotate(pic): width = getWidth(pic) height = getHeight(pic) new = makeEmptyPicture(height,width) tarX = 0 for x in range(0,width): tarY = 0 for y in range(0,height): p = getPixel(pic,x,y) color = getColor(p) setColor(getPixel(new,tarY,width-tarX-1),color) tarY = tarY + 1 tarX = tarX +1 show(new) return

Saving changes to newPic when copying an image in jython

Deadly 提交于 2019-12-11 04:23:56
问题 There are similar questions on Stack Overflow, but I cannot find what I am doing wrong in my code. def copyPic(): file=pickAFile() oldPic=makePicture(file) newPic=makeEmptyPicture(getWidth(oldPic),getHeight(oldPic)) for y in range(0,getHeight(oldPic)): for x in range(0,getWidth(oldPic)): oldPixel=getPixel(oldPic,x,y) colour=getColor(oldPixel) newPixel=getPixel(newPic,x,y) setColor(newPixel,colour) explore(newPic) When I use explore(newPic) or show(newPic) outside of the function, gives a

How to count the number of pixels of a certain color in python?

前提是你 提交于 2019-12-09 06:14:30
问题 I have a picture of two colours, black and red, and I need to be able to count how many pixels in the picture are red and how many are black. 回答1: I corrected code from 0xd3 to actually work: from PIL import Image im = Image.open('black.jpg') black = 0 red = 0 for pixel in im.getdata(): if pixel == (0, 0, 0): # if your image is RGB (if RGBA, (0, 0, 0, 255) or so black += 1 else: red += 1 print('black=' + str(black)+', red='+str(red)) 回答2: According to http://personal.denison.edu/~bressoud

Fading a picture gradually

荒凉一梦 提交于 2019-12-07 02:36:55
问题 The idea of this function is to fade the top half only of the picture (make it gradually darker). Here is what I have but it seems to be making all of the top half solid black. def fadeDownFromBlack(pic1): w=getWidth(pic1) h=getHeight(pic1) for y in range(0,h/2): for x in range(0,w): px=getPixel(pic1,x,y) setBlue(px,y*(2.0/h)) setRed(px,y*(2.0/h)) setGreen(px,y*(2.0/h)) 回答1: Let's look at just one line here: setBlue(px,y*(2.0/h)) and key part here is y*(2.0/h) y changes, as you go down. Let's